使用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>
一定要在布局文件中添加一个id为@android:id/list的ListView,否则有以下错误
java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
继承PreferenceActivity的类重写onCreate方法添加如下代码
setContentView(R.layout.setting_toolbar);
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
toolbar.setTitle("设置");
亲测有效的办法
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();
}
});
方法二:
继承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);
}
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>