Androd Toolbar 的简单使用(转)
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>
 
可以根据自己的需求,设置各种效果。但是,左边的边距一直去不了,如果知道的朋友,请给我留言,谢谢!