1. 程式人生 > >Toolbar爬坑歷程

Toolbar爬坑歷程

Toolbar的介紹我就不說了,最近沒有什麼事重新弄了一下Toolbar,畢竟之前沒有使用過。不用不知道,一用嚇一跳,出現了各種坑。網上查了各種文章、各種資料,一個個試了一遍終於實現了自己想要的效果。把網上的資料做個彙總。

簡單使用

首先需要修改你的 Theme,去除Actionbar的主題。

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize
your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item>
</style>

其中屬性 colorPrimary 、colorPrimaryDark 是可以修改 Toolbar 的背景,以及狀態列的背景顏色。這方面的資料網上已經很多,就不再這裡囉嗦了。

之後在佈局檔案中,引入Toolbar控制元件,為了相容低階版本的系統,需要引用 V7 包中的 Toolbar 控制元件。Toolbar是繼承 ViewGroup 的一個控制元件,也就意味著它能做其他控制元件的父控制元件。因此可以直接包含一個 TextView 實現文字居中的效果。程式碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="qhh.toolbardemo.MainActivity">
<android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:minHeight="?attr/actionBarSize" app:logo="@mipmap/ic_launcher" app:popupTheme="@style/ToolBarPopTheme" app:subtitle="subtitle" app:subtitleTextColor="@android:color/white" app:theme="@style/MyToolbarTheme" app:title="title" app:titleTextColor="@android:color/white"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textColor="@color/white" android:text="標題"/> </android.support.v7.widget.Toolbar> </LinearLayout>

今天在弄Toolbar的時候,就注意了這兩個屬性(因為坑了很久,真真的長了記性了)。

app:popupTheme="@style/ToolBarPopTheme"
app:theme="@style/MyToolbarTheme"

首先說說 popupTheme,該屬性是用來修改 Toolbar 彈出選單的樣式主題。

    <!--修改彈出框背景-->
    <!--<item name="android:actionOverflowButtonStyle">@style/OverFlowMenuStyle</item>-->
    <style name="ToolBarPopTheme" parent="@style/ThemeOverlay.AppCompat">
        <item name="android:colorBackground">#71f475</item>
        <item name="actionOverflowMenuStyle">@style/OverFlowMenuStyle</item>
    </style>

    <!--修改彈出框出現位置-->
    <style name="OverFlowMenuStyle" parent="Widget.AppCompat.Light.PopupMenu.Overflow">
        <item name="overlapAnchor">false</item>
    </style>

name=”android:colorBackground” 則是修改 menu 中的 item 的背景顏色。
name=”actionOverflowMenuStyle”OverFlowMenuStyle 是將彈出的選單框置於 Toolbar 之下。
這些都很容易就實現,但是在修改menu中的item的文字顏色的時候,則出現了問題。在網上找了很多方法,都沒有能實現。最後發現修改 menu item的文字顏色,並不在 popupTheme 中進行設定。而是在app:Theme 中設定。

    <style name="MyToolbarTheme" parent="@style/ThemeOverlay.AppCompat">
        <item name="actionMenuTextColor">@android:color/holo_blue_bright</item>
        <item name="android:textColorSecondary">@color/white</item>
        <item name="android:textColor">#0dccb9</item>
    </style>

如果設定的是 actionMenuTextColor 還是會發現,在menu中的item 的屬性是 app:showAsAction="never" 則會發現,所設定的顏色病不起作用,而屬性為 app:showAsAction=”ifRoom” 則有效果。網上各種找之後,找到了答案。設定 never 的item 字型顏色 應該使用的是 android:textColor

android:textColorSecondary 這個屬性則是設定,Toolbar 右邊三個預設小點的顏色的。也可以通過 <item name="android:actionOverflowButtonStyle">@style/OverFlowMenuStyle</item> 對預設的右邊圖片進行更換。

然而,在這裡遇到了今天最大的坑。在設定 android:textColorSecondary 的時候,最好不要放在 AppTheme 中。

<!-- Base application theme. -->
    <!--不應該 textColorSecondary 放在此段程式碼中-->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

而應該將其放在 Toolbar 自己的 Theme 中

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:logo="@mipmap/ic_launcher"
        app:popupTheme="@style/ToolBarPopTheme"
        app:subtitle="subtitle"
        app:subtitleTextColor="@android:color/white"
        app:theme="@style/MyToolbarTheme"
        app:title="title"
        app:titleTextColor="@android:color/white">

<style name="MyToolbarTheme" parent="@style/ThemeOverlay.AppCompat">
        <item name="actionMenuTextColor">@android:color/holo_blue_bright</item>
        <item name="android:textColorSecondary">@color/white</item>
        <item name="android:textColor">#0dccb9</item>
    </style>

為什麼呢?因為今天在使用 NavigationView 和Toolbar 一起聯合使用的時候報錯了,無法解析載入xml檔案,功底淺,不明白具體的原理。最後在 stackoverflow 中找到問題所在, stackoverflow中有人提出,最好不要在 AppTheme 中設定類似的屬性,在想是不是相容性的問題呢?最後,經過嘗試將其使用在 Toolbar的 Theme中就沒有問題了,不知道哪位看官大神能詳細的解釋一下。