日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android PreferenceActivity添加ToolBar

發(fā)布時間:2023/12/9 Android 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android PreferenceActivity添加ToolBar 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

使用PreferenceActivity實(shí)現(xiàn)設(shè)置界面,發(fā)現(xiàn)沒有Toolbar,非常難看,與程序的界面不統(tǒng)一

如何在PreferenceActivity添加ToolBar?
在網(wǎng)絡(luò)上搜索到以下方法
方法一:
新建Layout文件,包含Toolbar

<?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.support.v7.widget.Toolbarandroid: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"/><!--這里必須能添加一個id為@android:id/list的ListView否則會報(bào)錯--><ListViewandroid:id="@android:id/list"android:layout_width="match_parent"android:layout_height="match_parent">az</ListView> </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("設(shè)置");

親測有效的辦法
PreferenceActivity的ToolBar添加返回按鈕
對于PreferenceActivity,不是繼承AppCompatActivity,無法使用getSupportActionBar().setDisplayHomeAsUpEnabled(true)進(jìn)行返回按鈕的設(shè)置,但是可以將左側(cè)導(dǎo)航圖標(biāo)設(shè)置成返回按鈕并添加按鍵事件進(jìn)行實(shí)現(xiàn)
首先在drawable文件夾添加一個返回圖標(biāo)ic_arrow_back_black_24dp
在代碼中進(jìn)行設(shè)置

Drawable drawable=getResources().getDrawable(R.drawable.ic_arrow_back_black_24dp);toolbar.setNavigationIcon(drawable);toolbar.setNavigationOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {finish();}});

方法二:
繼承PreferenceActivity重寫onCreate方法添加如下代碼:

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setStatusBar();//找到Activity根布局ViewGroup rootView = (ViewGroup) findViewById(android.R.id.content);//獲取根布局子ViewView content = rootView.getChildAt(0);//加載自定義布局文件LinearLayout toolbarLayout = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.activity_toolbar, null);//移除根布局所有子viewrootView.removeAllViews();//注意這里一要將前面移除的子View添加到我們自定義布局文件中,否則PreferenceActivity中的Header將不會顯示toolbarLayout.addView(content);//將包含Toolbar的自定義布局添加到根布局中rootView.addView(toolbarLayout);//設(shè)置toolbarToolbar toolbar=(Toolbar)toolbarLayout.findViewById(R.id.toolbar);toolbar.setTitle("設(shè)置");toolbar.setTitleTextColor(Color.WHITE);Drawable d=getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);toolbar.setNavigationIcon(d); }

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。