Android Toolbar自定義標題標題居中的實例代碼
自定義Toolbar,實現(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="標題" android:textColor="@color/group_button_dialog_pressed_holo_light" android:textSize="22sp" /> </android.support.v7.widget.Toolbar> .......
除了代碼中貼出的TextView,在Toolbar中可以加入其它控件,自定義標題欄 。
2. 使用AppTheme去掉ActionBar或者 使用Theme.AppCompat.NoActionBar主題
• 否則會出現(xiàn)錯誤信息:
• 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.
錯誤原因:由于Activity已存在ActionBar,所以使用Toolbar進行替換時出錯
•使用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);
}
只兩句,無需設(shè)置title為空字符串。
4.效果

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

