最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android Toolbar自定義標題標題居中的實例代碼

 更新時間:2018年08月09日 10:50:00   作者:Morgandas  
這篇文章主要介紹了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)文章

最新評論

固原市| 南江县| 绍兴市| 云和县| 新乐市| 贵南县| 河北省| 小金县| 汉中市| 玉林市| 竹山县| 潼关县| 灵武市| 彩票| 玉山县| 益阳市| 五莲县| 宾阳县| 黄石市| 万年县| 清涧县| 芜湖市| 乐都县| 广汉市| 万安县| 张北县| 大渡口区| 三原县| 湖南省| 泰宁县| 天水市| 泸定县| 东光县| 苍溪县| 临汾市| 惠水县| 南皮县| 太和县| 西畴县| 舒兰市| 林州市|