欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)文章

  • 關(guān)于RxJava的一些特殊用法小結(jié)

    關(guān)于RxJava的一些特殊用法小結(jié)

    RxJava 是一個響應(yīng)式編程框架,采用觀察者設(shè)計模式。下面這篇文章主要總結(jié)介紹了一些關(guān)于RxJava的特殊用法,需要的朋友可以參考借鑒,下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。
    2017-05-05
  • Android 實現(xiàn)手機撥打電話的功能

    Android 實現(xiàn)手機撥打電話的功能

    本篇文章主要介紹 Android 開發(fā)手機撥打電話的功能,這里提供示例代碼,有興趣的小伙伴可以參考下
    2016-08-08
  • Android?TextView冷門實用方法技巧

    Android?TextView冷門實用方法技巧

    這篇文章主要為大家介紹了Android?TextView冷門實用方法技巧示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • 給大家分享一些安卓自學(xué)心得

    給大家分享一些安卓自學(xué)心得

    本文是筆者在學(xué)習(xí)安卓開發(fā)的過程中的一些經(jīng)驗之談的分享,希望對大家學(xué)習(xí)安卓開發(fā)能夠有所幫助。
    2015-12-12
  • Flutter交互并使用小工具管理其狀態(tài)widget的state詳解

    Flutter交互并使用小工具管理其狀態(tài)widget的state詳解

    這篇文章主要為大家介紹了Flutter交互并使用小工具管理其狀態(tài)widget的state詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • Android 進程間通信實現(xiàn)原理分析

    Android 進程間通信實現(xiàn)原理分析

    只有你允許客戶端從不同的應(yīng)用程序為了進程間的通信而去訪問你的service,以及想在你的service處理多線程,下面為大家詳細介紹下
    2013-06-06
  • Android ActionBar使用教程

    Android ActionBar使用教程

    這篇文章主要為大家分享了Android ActionBar使用教程,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Android仿微信通訊錄列表側(cè)邊欄效果

    Android仿微信通訊錄列表側(cè)邊欄效果

    這篇文章主要為大家詳細介紹了Android仿微信通訊錄列表側(cè)邊欄效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • Android GridLayout使用案例詳解

    Android GridLayout使用案例詳解

    這篇文章主要介紹了Android GridLayout使用案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Android 顯示刷新頻率的實現(xiàn)代碼

    Android 顯示刷新頻率的實現(xiàn)代碼

    這篇文章主要介紹了Android 顯示刷新頻率的實現(xiàn)代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-08-08

最新評論