国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android Toolbar自定義標(biāo)題標(biāo)題居中的實(shí)例代碼

2019-10-21 21:41:56
字體:
供稿:網(wǎng)友

自定義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); }

只兩句,無需設(shè)置title為空字符串。

4.效果

Android,Toolbar,自定義標(biāo)題

總結(jié)

以上所述是小編給大家介紹的Android Toolbar自定義標(biāo)題標(biāo)題居中,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)VEVB武林網(wǎng)網(wǎng)站的支持!


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 阳曲县| 佛坪县| 贡觉县| 娱乐| 富宁县| 缙云县| 克东县| 百色市| 巴青县| 府谷县| 彭州市| 林口县| 苗栗县| 梁山县| 汉川市| 仙游县| 重庆市| 葵青区| 固阳县| 定西市| 太康县| 平谷区| 武夷山市| 阿图什市| 永福县| 新丰县| 泸定县| 海口市| 桂东县| 鹤山市| 行唐县| 精河县| 谢通门县| 思南县| 阳谷县| 中方县| 洪湖市| 小金县| 绥德县| 云阳县| 东宁县|