如何製作客制化Appbar


Android上方工具列的名字,從ActionBar改名為Toolbar之後,又再加入新的定義名稱Appbar。所以這邊就使用最新的名稱為Appbar。
由於Android的設計規範是將Appbar的文字標題或icon偏左,但又常常會遇到美術設計出的圖是將文字標題或icon置中,這個時候就只能自行客制了。


一、使用NoToolBar的style
<style name="NewAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/toolbar_background_gray</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
</style>

二、自行建立Toolbar, 將圖片置中
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:minHeight="?attr/actionBarSize"  
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:titleTextColor="@android:color/white"
    android:background="?attr/colorPrimary">

     <ImageView
            android:layout_width="wrap_content"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="center"
            android:padding="17dp"
            android:src="@drawable/logo_google" />

</android.support.v7.widget.Toolbar>

三、如果沒有使用NoToolBar的style會發生的事
當你決定使用客制化的Appbar的Layout, 如果你沒有使用NoToolBar的style,在畫面上會發生有兩個Appbar的情形

而且如果你有使用了自行增加Toolbar的程式碼的時候
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

就會發生錯誤而閃退
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.

四、加入陰影
由於做到步驟二的時候,雖然已經可以產生客制化的Toolbar了,但是沒有預設的陰影,所以再使用AppBarLayout包起來就可以產生了
<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:titleTextColor="@android:color/white">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="center"
            android:padding="17dp"
            android:src="@drawable/logo" />

    </android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

五、完成結果
reference:
https://guides.codepath.com/android/Using-the-App-Toolbar
http://stackoverflow.com/questions/26515058/this-activity-already-has-an-action-bar-supplied-by-the-window-decor

留言

  1. 想請文你照片最下面的工具列是怎樣顯示的?是gridview嗎?還是其他?謝謝

    回覆刪除
    回覆
    1. 那個是Android的一個UI元件:BottomNavigationView

      刪除

張貼留言

這個網誌中的熱門文章

ISO 27001 LA 主導稽核員 考照心得

Android如何實作強制App版本更新

如何實作從API抓取資料顯示在列表頁(ListView)上