Android Toolbar自定義標(biāo)題標(biāo)題居中的實(shí)例代碼
自定義Toolbar,實(shí)現(xiàn)方式如下:
1.布局文件,在activity_main.xml 文件中寫入Toolbar
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"> <android.support.v7.widget.Toolbar android:id="@+id/main_toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?attr/actionBarSize" android:background="@color/colorblue" app:contentInsetStart="0dp" android:layout_gravity="center" app:popupTheme="@style/AppTheme.PopupOverlay"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="標(biāo)題" android:textColor="@color/group_button_dialog_pressed_holo_light" android:textSize="22sp" /> </android.support.v7.widget.Toolbar> .......
除了代碼中貼出的TextView,在Toolbar中可以加入其它控件,自定義標(biāo)題欄 。
2. 使用AppTheme去掉ActionBar或者 使用Theme.AppCompat.NoActionBar主題
• 否則會(huì)出現(xiàn)錯(cuò)誤信息:
• Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
錯(cuò)誤原因:由于Activity已存在ActionBar,所以使用Toolbar進(jìn)行替換時(shí)出錯(cuò)
•使用AppTheme去掉ActionBar
修改values文件下style.xml中的AppTheme屬性,代碼如下:
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="windowActionBar">false</item> <item name="android:windowActionBar">false</item> <item name="android:windowNoTitle">true</item> <item name="windowNoTitle">true</item> </style>
• 或使用Theme.AppCompat.NoActionBar主題
AndroidManifest.xml文件中設(shè)置
android:theme="@style/AppTheme.NoActionBar"
3.activity代碼中設(shè)置Toolbar
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar mToolbar = (Toolbar) findViewById(R.id.main_toolbar);
setSupportActionBar(mToolbar);
}
只兩句,無(wú)需設(shè)置title為空字符串。
4.效果

總結(jié)
以上所述是小編給大家介紹的Android Toolbar自定義標(biāo)題標(biāo)題居中,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Android實(shí)現(xiàn)彈窗進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)彈窗進(jìn)度條效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android8.1原生系統(tǒng)網(wǎng)絡(luò)感嘆號(hào)消除的方法
這篇文章主要介紹了Android8.1原生系統(tǒng)網(wǎng)絡(luò)感嘆號(hào)消除的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05
Android中的Dalvik和ART詳解及區(qū)別分析
小編通過(guò)這篇文章給大家整理了什么是Dalvik和ART,并進(jìn)行了區(qū)別的分析,下面一起來(lái)看看。2016-07-07
Android Room數(shù)據(jù)庫(kù)自動(dòng)升級(jí)與遷移的策略
在 Android 應(yīng)用開發(fā)中,Room 是 Google 提供的一個(gè)輕量級(jí)數(shù)據(jù)庫(kù)框架,用于簡(jiǎn)化與 SQLite 的交互,本文將介紹 Room 數(shù)據(jù)庫(kù)升級(jí)的幾種場(chǎng)景和常見(jiàn)的處理方法,包括手動(dòng)遷移和自動(dòng)遷移的策略,需要的朋友可以參考下2024-09-09
Android——Android lint工具項(xiàng)目資源清理詳解
這篇文章主要介紹了Android——Android lint工具項(xiàng)目資源清理詳解的相關(guān)資料,需要的朋友可以參考下2016-11-11
基于Flutter實(shí)現(xiàn)轉(zhuǎn)場(chǎng)動(dòng)效的示例代碼
動(dòng)畫經(jīng)常會(huì)用于場(chǎng)景切換,比如滑動(dòng),縮放,尺寸變化。Flutter?提供了Transition系列的動(dòng)畫組件,可以讓場(chǎng)景轉(zhuǎn)換動(dòng)畫變得更加簡(jiǎn)單。本文整理了常用的Transition組件的應(yīng)用,需要的可以參考一下2022-05-05
Android編程實(shí)現(xiàn)變化的雙重選擇框功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)變化的雙重選擇框功能,結(jié)合實(shí)例形式分析了Android雙重選擇框功能的樣式布局與功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-10-10
設(shè)置Android設(shè)備WIFI在休眠時(shí)永不斷開的代碼實(shí)現(xiàn)
這篇文章主要介紹了設(shè)置Android設(shè)備WIFI在休眠時(shí)永不斷開的代碼實(shí)現(xiàn),需要的朋友可以參考下2014-07-07
Android XRecyclerView最簡(jiǎn)單的item點(diǎn)擊事件處理
這篇文章主要為大家詳細(xì)介紹了Android XRecyclerView最簡(jiǎn)單的item點(diǎn)擊事件處理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12

