• Android PreferenceActivity添加ToolBar


    使用PreferenceActivity实现设置界面,发现没有Toolbar,非常难看,与程序的界面不统一
    在这里插入图片描述
    如何在PreferenceActivity添加ToolBar?
    在网络上搜索到以下方法
    方法一:
    新建Layout文件,包含Toolbar

    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:orientation="vertical">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:background="@color/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>
        
        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent">azListView>
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    一定要在布局文件中添加一个id为@android:id/list的ListView,否则有以下错误

    java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
    
    • 1

    继承PreferenceActivity的类重写onCreate方法添加如下代码

            setContentView(R.layout.setting_toolbar);
            Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
            toolbar.setTitle("设置");
    
    • 1
    • 2
    • 3

    亲测有效的办法
    PreferenceActivity的ToolBar添加返回按钮
    对于PreferenceActivity,不是继承AppCompatActivity,无法使用getSupportActionBar().setDisplayHomeAsUpEnabled(true)进行返回按钮的设置,但是可以将左侧导航图标设置成返回按钮并添加按键事件进行实现
    首先在drawable文件夹添加一个返回图标ic_arrow_back_black_24dp
    在代码中进行设置

            Drawable drawable=getResources().getDrawable(R.drawable.ic_arrow_back_black_24dp);
            toolbar.setNavigationIcon(drawable);
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    方法二:
    继承PreferenceActivity重写onCreate方法添加如下代码:

    @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setStatusBar();
          //找到Activity根布局
          ViewGroup rootView = (ViewGroup) findViewById(android.R.id.content);
          //获取根布局子View
          View content = rootView.getChildAt(0);
          //加载自定义布局文件
          LinearLayout toolbarLayout = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.activity_toolbar, null);
           //移除根布局所有子view
          rootView.removeAllViews();
          //注意这里一要将前面移除的子View添加到我们自定义布局文件中,否则PreferenceActivity中的Header将不会显示
          toolbarLayout.addView(content);
          //将包含Toolbar的自定义布局添加到根布局中
          rootView.addView(toolbarLayout);
          //设置toolbar
          Toolbar toolbar=(Toolbar)toolbarLayout.findViewById(R.id.toolbar);
          toolbar.setTitle("设置");
          toolbar.setTitleTextColor(Color.WHITE);
          Drawable d=getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
          toolbar.setNavigationIcon(d);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    activity_toolbar.xml内容:

    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:orientation="vertical">
    
        <android.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize">
        android.widget.Toolbar>
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    参考:
    在PreferenceActivity中使用ToolBar

  • 相关阅读:
    Android Framework系列---输入法服务
    计算机毕业设计Python+djang的疫情防控下医院人员调动系统(源码+系统+mysql数据库+Lw文档)
    MongoDB打破了原则引入SQL?
    STM32 EtherCAT 总线型(1 拖 4)步进电机解决方案
    java计算机毕业设计高校墨香文学社管理系统MyBatis+系统+LW文档+源码+调试部署
    使用tornado实现sse
    python内置模块typing 类型提示
    【Elasticsearch】在es中实现mysql中的FIND_IN_SET查询条件
    微服务自动化【集群搭建】
    流量抓取工具(wireshark)
  • 原文地址:https://blog.csdn.net/m0_60352504/article/details/126464105