14年Android開發者大會提出了Android5.0 系統以及 材料設置 Material Design。在 材料設計中推出了大量的UI效果,其中某些功能 已添加進 兼容包,所以可以在低版本中來實現一些材料設計效果。今天主要介紹的就是 ActionBar的替代品 Toolbar。Toolbar 是在V7包兼容的,所以需要下載最新的V7包。
使用ToolBar主要從以下3個步驟開始:
- 樣式定義顯示效果,屬性
- 在布局中使用ToolBar
- 在代碼中設置屬性
1. 首先,來看下如何設置樣式。由於Toolbar是替代ActionBar的,還需要使用 ActionBar兼容的Theme,但是需要做一些簡單修改。
a. 先把ActionBar隱藏,為了 方便修改可以將原本 AppTheme 的 parent 屬性改為 AppTheme.Base,修改文件 res/values/styles.xml
- <resources>
- <style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
- <item name="windowActionBar">false</item>
- <item name="android:windowNoTitle">true</item>
- </style>
- <style name="AppTheme" parent="AppTheme.Base"/>
- </resources>
b. 同樣,為統一風格,需要將 /res/values-v21/styles.xml 的樣式 parent 屬性設置為 AppTheme.Base
- <resources>
- <style name="AppTheme" parent="AppTheme.Base"></style>
- </resources>
2. 在 Activity 布局中 加入 Toolbar 組件。注意:為了向下兼容,要使用 V7包下的 Toolbar,並且去掉最外層布局的padding屬性。為了確保Toolbar的高度,需要設置 最小高度為ActionBar高度 android:minHeight="?attr/actionBarSize"。
- <RelativeLayout 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"
- tools:context=".MainActivity">
- <android.support.v7.widget.Toolbar
- android:layout_width="match_parent"
- android:layout_height="<span style="font-family: Arial, Helvetica, sans-serif;">?attr/actionBarSize</span><span style="font-family: Arial, Helvetica, sans-serif;">"</span>
- android:id="@+id/toolbar" />
- </RelativeLayout>
3. 在代碼中設置Toolbar。需要Activity 繼承 ActionBarActivity。通過setSupportActionBar()替代ActionBar,在OnCreate() 方法中加入以下代碼。
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
完成之后,效果如下:
這個 效果不怎么好,需要對其進行簡單的修改,改變一些顏色,修整修整就可以見人了。修改的顏色可以參見下圖。
通過上圖,不難發現,修改顏色主要從這幾個方面開始:
- 狀態欄背景色 colorPrimaryDark 屬性
- 如果還是使用ActionBar,colorPrimary 屬性設置背景 如果時Toolbar,布局中background 屬性設置背景
- 導航欄背景色 navigationBarColor 屬性 ,需要在 5.0 才可以使用,所以屬性只可以在 /res/values-v21/styles.xml 設置
- 主界面背景色 windowBackground
- <style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
- <item name="windowActionBar">false</item>
- <item name="android:windowNoTitle">true</item>
- <item name="colorPrimary">#66cc99</item>
- <item name="colorPrimaryDark">#66cc99</item>
- <item name="android:windowBackground">@color/back</item>
- </style>
- <style name="AppTheme" parent="AppTheme.Base">
- <item name="android:navigationBarColor">#66cc99</item>
- </style>
- <android.support.v7.widget.Toolbar
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/toolbar"
- android:background="#339999"
- android:minHeight="?attr/actionBarSize" />
設置完成后,效果如下:

上一篇簡單的介紹了如何簡單使用Toolbar,這篇主要介紹Toolbar的進一步設置。
既然Toolbar要替代ActionBar,那么Toolbar的功能應該更為強大,在Toolbar上有一些默認的顯示效果,先來看下。
通過上圖,不難看出,我們其實是可以為Toolbar設置以下屬性的:
- 上級按鈕 (upbutton) setNavigationIcon
- APP 的圖標 setLogo
- 主標題 setTitle
- 副標題 setSubtitle
- 設定菜單各按鈕的動作 setOnMenuItemClickListener
- toolbar.setLogo(R.drawable.ic_launcher);
- toolbar.setNavigationIcon(R.drawable.ic_launcher);
- toolbar.setTitle(getResources().getString(R.string.app_name));
- toolbar.setSubtitle("ToolBar");
- toolbar.setOnMenuItemClickListener(this);
- toolbar.setTitleTextColor(0xffffffff);
- toolbar.setSubtitleTextColor(0xffffffff);
注意:setNavigationIcon(),setOnMenuItemClickListener() 需要放在 setSupportActionBar之后才會生效
Toolbar菜單效果與ActionBar的實現一樣,都是OptionsMenu。需要在Menu中添加 item ,然后通過Toolbar顯示出來。
res/menu/menu_main.xml
- <menu 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"
- tools:context=".MainActivity">
- <item
- android:id="@+id/action_settings"
- android:title="@string/action_settings"
- android:orderInCategory="100"
- android:icon="@drawable/ic_launcher"
- app:showAsAction="always" />
- <item
- android:id="@+id/action_test"
- android:title="@string/action_settings"
- android:orderInCategory="10"
- android:icon="@drawable/ic_launcher"
- app:showAsAction="ifRoom" />
- </menu>
然后在MainActivity中添加以下代碼
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onMenuItemClick(MenuItem menuItem) {
- Toast.makeText(this, menuItem.getTitle(), Toast.LENGTH_SHORT).show();
- return false;
- }
運行效果如下:
通過點擊 菜單,可以發現能夠觸發 onMenuItemClick() 方法,但是,點擊上級按鈕 (upbutton)並沒有觸發該事件,因為它有自己獨立的點擊事件。
- toolbar.setNavigationOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(MainActivity.this, "HOME", Toast.LENGTH_SHORT).show();
- }
- });
在Android 原生樣式應用中,有一個特別漂亮的效果,在使用抽屜布局的時候,展開或關閉抽屜時,Toolbar的 navigation drawer(upButton) 有一個動畫,由三個橫線旋轉成箭頭,大概如下:
靜態圖片展示不出來動畫效果,請自行補腦!
這個效果其實是由 Toolbar+DrawerLayout 實現的。
可以通過以下幾步實現:
- 在布局中加入DrawerLayout
- 在代碼中找到DrawerLayout,將DrawerLayout 與 Toolbar關聯
- 通過樣式修改 navigation drawer 的顏色
- <RelativeLayout 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"
- tools:context=".MainActivity">
- <android.support.v7.widget.Toolbar
- android:layout_width="match_parent"
- android:layout_height="?attr/actionBarSize"
- android:id="@+id/toolbar"
- android:background="#339999" />
- <android.support.v4.widget.DrawerLayout
- android:layout_below="@+id/toolbar"
- android:id="@+id/drawerlayou"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <!--Main Content-->
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#123456">
- </LinearLayout>
- <!--DrawerLayout Content-->
- <LinearLayout
- android:layout_width="300dp"
- android:layout_height="match_parent"
- android:background="#345678"
- android:layout_gravity="start" />
- </android.support.v4.widget.DrawerLayout>
- </RelativeLayout>
- final DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawerlayou);
- ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.app_name, R.string.app_name);
- toggle.syncState();
- drawerLayout.setDrawerListener(toggle);
- //通過 NavigationDrawer 打開關閉 抽屜
- toolbar.setNavigationOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(MainActivity.this, "HOME", Toast.LENGTH_SHORT).show();
- if (drawerLayout.isDrawerOpen(Gravity.START))
- drawerLayout.closeDrawer(Gravity.START);
- else
- drawerLayout.openDrawer(Gravity.START);
- }
- });
- <resources>
- <style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
- <item name="windowActionBar">false</item>
- <item name="android:windowNoTitle">true</item>
- <item name="colorPrimary">#66cc99</item>
- <item name="colorPrimaryDark">#66cc99</item>
- <item name="android:windowBackground">@color/back</item>
- <!-- 使用自定義樣式-->
- <item name="drawerArrowStyle">@style/AppTheme.MyDrawerArrowStyle</item>
- </style>
- <style name="AppTheme" parent="AppTheme.Base" />
- <!--自定義 navigation drarwer 的樣式-->
- <style name="AppTheme.MyDrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
- <item name="spinBars">false</item>
- <item name="color">#FFFFFF</item>
- </style>
- </resources>
如果想要抽屜遮住 Toolbar,只需要改下布局,將Toolbar放入DrawerLayout的主界面即可:
- <android.support.v4.widget.DrawerLayout
- android:id="@+id/drawerlayou"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <!--Main Content-->
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#123456">
- <!-- android:elevation="5dp" 加上陰影-->
- <android.support.v7.widget.Toolbar
- android:layout_width="match_parent"
- android:layout_height="?attr/actionBarSize"
- android:id="@+id/toolbar"
- android:elevation="5dp"
- android:background="#339999" />
- </LinearLayout>
- <!--DrawerLayout Content-->
- <LinearLayout
- android:layout_width="300dp"
- android:layout_height="match_parent"
- android:background="#345678"
- android:layout_gravity="start" />
- </android.support.v4.widget.DrawerLayout>
Toolbar 相對於 ActionBar的強大之處在於,ToolBar有更強大的自定義效果。因為ToolBar本身就是一個ViewGroup,可以往Toolbar中放入各種組件。
- <android.support.v7.widget.Toolbar
- android:layout_width="match_parent"
- android:layout_height="?attr/actionBarSize"
- android:id="@+id/toolbar"
- android:elevation="5dp"
- android:background="#339999">
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#F00">
- <Button
- android:id="@+id/toolbar_btn"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:text="Button"/>
- <ImageView
- android:layout_width="20dp"
- android:layout_height="20dp"
- android:src="@drawable/ic_launcher"
- android:layout_centerInParent="true" />
- <TextView
- android:layout_alignParentRight="true"
- android:layout_centerVertical="true"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="鄧紫棋" />
- </RelativeLayout>
可以根據自己的需求,設置各種效果。但是,左邊的邊距一直去不了,如果知道的朋友,請給我留言,謝謝!