Android PreferenceActivity添加ToolBar
使用PreferenceActivity實現(xiàn)設(shè)置界面,發(fā)現(xiàn)沒有Toolbar,非常難看,與程序的界面不統(tǒng)一
如何在PreferenceActivity添加ToolBar?
在網(wǎng)絡(luò)上搜索到以下方法
方法一:
新建Layout文件,包含Toolbar
一定要在布局文件中添加一個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("設(shè)置");親測有效的辦法
PreferenceActivity的ToolBar添加返回按鈕
對于PreferenceActivity,不是繼承AppCompatActivity,無法使用getSupportActionBar().setDisplayHomeAsUpEnabled(true)進(jìn)行返回按鈕的設(shè)置,但是可以將左側(cè)導(dǎo)航圖標(biāo)設(shè)置成返回按鈕并添加按鍵事件進(jìn)行實現(xiàn)
首先在drawable文件夾添加一個返回圖標(biāo)ic_arrow_back_black_24dp
在代碼中進(jìn)行設(shè)置
方法二:
繼承PreferenceActivity重寫onCreate方法添加如下代碼:
activity_toolbar.xml內(nèi)容:
<?xml version="1.0" encoding="utf-8"?> <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.Toolbarandroid: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>參考:
在PreferenceActivity中使用ToolBar
總結(jié)
以上是生活随笔為你收集整理的Android PreferenceActivity添加ToolBar的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [vue-element] Elemen
- 下一篇: [css] 写出固定子容器在固定的父容器