[关闭]
@xujun94 2016-09-21T13:41:40.000000Z 字数 6450 阅读 1428

ToolBar

本篇博客主要介绍以下内容

转载请注明原博客地址: http://blog.csdn.net/gdutxiaoxu/article/details/52602327

例子源码下载地址: http://download.csdn.net/detail/gdutxiaoxu/9635393

介绍

先来看一下官方的介绍

A Toolbar is a generalization of action bars for use within application layouts. While an action bar is traditionally part of an Activity's opaque window decor controlled by the framework, a Toolbar may be placed at any arbitrary level of nesting within a view hierarchy. An application may choose to designate a Toolbar as the action bar for an Activity using the setSupportActionBar() method.

简单来说就是Toolbar 是在 Android 5.0 开始推出的一个 Material Design 风格的导航控件 ,Google 非常推荐大家使用 Toolbar 来作为Android客户端的导航栏,以此来取代之前的 Actionbar 。

那么ToolBar能干什么呢
- A navigation button,设置导航栏图标
- A branded logo image. 设置APP logo图标
- A title and subtitle. 设置标题和子标题
- One or more custom views. 设置一个或者多个自定义View
- An action menu. 设置一个ActionMenu

基本使用

首先我们需要先了解一下知识

怎样修改导航栏的颜色?

  1. compile 'com.android.support:appcompat-v7:24.2.1'
  1. <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
  2. <!-- Customize your theme here. -->
  3. <item name="colorPrimary">@color/colorPrimary</item>
  4. <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  5. <item name="colorAccent">@color/colorAccent</item>
  6. </style>
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. xmlns:toolbar="http://schemas.android.com/apk/res-auto"
  7. >
  8. <android.support.v7.widget.Toolbar
  9. android:id="@+id/toolBar"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:background="?attr/colorPrimary"
  13. >
  14. </android.support.v7.widget.Toolbar>
  15. </RelativeLayout>

运行以上代码将可以看到以下效果图


如何为toolBar添加内容

总共有两种方法,

第一种方法方法,在布局文件里面

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:toolbar="http://schemas.android.com/apk/res-auto"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. >
  8. <android.support.v7.widget.Toolbar
  9. android:id="@+id/toolBar"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:background="?attr/colorPrimary"
  13. toolbar:logo="@mipmap/ic_launcher"
  14. toolbar:navigationIcon="@mipmap/me"
  15. toolbar:subtitle="subtitle"
  16. toolbar:title="@string/app_name"
  17. >
  18. </android.support.v7.widget.Toolbar>
  19. </RelativeLayout>

注意事项

要使用

  1. xmlns:toolbar="http://schemas.android.com/apk/res-auto"
  2. toolbar:logo="@mipmap/ic_launcher"
  3. toolbar:navigationIcon="@mipmap/me"
  4. toolbar:subtitle="subtitle"
  5. toolbar:title="@string/app_name"

而不是

  1. android:logo="@mipmap/ic_launcher"
  2. android:navigationIcon="@mipmap/me"
  3. android:subtitle="subtitle"
  4. android:title="@string/app_name"

第二种方法,在java代码里面

  1. toolbar = (Toolbar) findViewById(R.id.toolBar);
  2. toolbar.setTitle("Title");
  3. toolbar.setSubtitle("SubTitle");
  4. toolbar.setLogo(R.mipmap.ic_launcher);
  5. //设置导航图标要在setSupportActionBar方法之后
  6. setSupportActionBar(toolbar);
  7. toolbar.setNavigationIcon(R.mipmap.ic_launcher);

将可以看到如下效果图

为toolBar添加Menu点击事件

  1. <menu xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. tools:context=".MainActivity">
  5. <item
  6. android:id="@+id/action_search"
  7. android:icon="@drawable/search"
  8. android:orderInCategory="80"
  9. android:title="查找"
  10. app:showAsAction="always" />
  11. <item
  12. android:id="@+id/action_share"
  13. android:icon="@drawable/share"
  14. android:orderInCategory="90"
  15. android:title="分享"
  16. app:showAsAction="ifRoom" />
  17. <item
  18. android:id="@+id/action_settings"
  19. android:orderInCategory="100"
  20. android:title="设置"
  21. app:showAsAction="never" />
  22. <item
  23. android:id="@+id/action_about"
  24. android:orderInCategory="110"
  25. android:title="关于"
  26. app:showAsAction="never" />
  27. </menu>
  1. @Override
  2. public boolean onCreateOptionsMenu(Menu menu) {
  3. getMenuInflater().inflate(R.menu.menu_main,menu);
  4. return super.onCreateOptionsMenu(menu);
  5. }
  1. toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
  2. @Override
  3. public boolean onMenuItemClick(MenuItem item) {
  4. switch (item.getItemId()){
  5. case R.id.action_settings:
  6. ToastUtils.showText(NormalToolBarActivity.this,"setting");
  7. return true;
  8. case R.id.action_about:
  9. ToastUtils.showText(NormalToolBarActivity.this,"about");
  10. return true;
  11. case R.id.action_search:
  12. ToastUtils.showText(NormalToolBarActivity.this,"serch");
  13. break;
  14. case R.id.action_share:
  15. ToastUtils.showText(NormalToolBarActivity.this,"share");
  16. break;
  17. case android.R.id.home:
  18. ToastUtils.showText(NormalToolBarActivity.this,"home");
  19. break;
  20. }
  21. return false;
  22. }
  23. });

运行上述代码,可以看到以下效果图

拓展

1)各种属性讲解

引用地址来源:http://blog.csdn.net/feiduclear_up/article/details/46457433

以上效果的主题配置如下:

  1. <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
  2. <!-- Customize your theme here. -->
  3. <!--导航栏底色-->
  4. <item name="colorPrimary">@color/accent_material_dark</item>
  5. <!--状态栏底色-->
  6. <item name="colorPrimaryDark">@color/accent_material_light</item>
  7. <!--导航栏上的标题颜色-->
  8. <item name="android:textColorPrimary">@android:color/black</item>
  9. <!--Activity窗口的颜色-->
  10. <item name="android:windowBackground">@color/material_blue_grey_800</item>
  11. <!--按钮选中或者点击获得焦点后的颜色-->
  12. <item name="colorAccent">#00ff00</item>
  13. <!--和 colorAccent相反,正常状态下按钮的颜色-->
  14. <item name="colorControlNormal">#ff0000</item>
  15. <!--Button按钮正常状态颜色-->
  16. <item name="colorButtonNormal">@color/accent_material_light</item>
  17. <!--EditText 输入框中字体的颜色-->
  18. <item name="editTextColor">@android:color/white</item>
  19. </style>

1.colorPrimary: Toolbar导航栏的底色。
2.colorPrimaryDark:状态栏的底色,注意这里只支持Android5.0以上的手机。
3.textColorPrimary:整个当前Activity的字体的默认颜色。
4.android:windowBackground:当前Activity的窗体颜色。
5.colorAccent:CheckBox,RadioButton,SwitchCompat等控件的点击选中颜色
6.colorControlNormal:CheckBox,RadioButton,SwitchCompat等默认状态的颜色。
7.colorButtonNormal:默认状态下Button按钮的颜色。
8.editTextColor:默认EditView输入框字体的颜色。

修改toolBar menu的样式

在默认的情况下,弹出menu的位置是这样的

其实大家也不难想到要修改popupTheme的样式,首先我们现在styles中修改为我们想要的样式.

  1. <style name="ToolbarPopupTheme" parent="@style/ThemeOverlay.AppCompat.Dark">
  2. <item name="android:colorBackground">#000000</item>
  3. <item name="actionOverflowMenuStyle">@style/OverflowMenuStyle</item> <!--新增一个item,用于控制menu-->
  4. </style>
  5. <style name="OverflowMenuStyle" parent="Widget.AppCompat.Light.PopupMenu.Overflow">
  6. <item name="overlapAnchor">false</item> <!--把该属性改为false即可使menu位置位于toolbar之下-->
  7. </style>

接着再在toolBar目录下修改我们toolBar的样式.

  1. toolbar:popupTheme="@style/ToolbarPopupTheme"

修改之后的menu的样式是这样的


题外话

本次学习toolBar的使用,主要是想修改最上面状态栏的颜色,google了一下发现与toolBar相关,便学习了一下,相对来说这篇博客是比较枯燥的,很多东西都是要查阅相关文档的,用起来比较繁琐。对于 本人来说,还是习惯不用actionBar,用自定义的title比较顺手。当然,每个人都有自己的习惯,适合自己的才是最好的,了解toolBar也能了解多一种实现方式。

  1. ```
  2. ```java
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注