Toolbar

你还在为Android 的ActionBar的文字不能随意设置位置而烦恼么?你还在为ActionBar不能自定义添加自己的布局而烦恼么?现在告诉你一个好消息,当你看到这篇文章时,就不必烦恼了。Google在Android5.0以后推出了一个Toolbar来完全代替之前的Actionbar,Toolbar的出现解决了Actionbar的各种限制,Toolbar可以完全自定义和配置。我们从以下几个点了解Toolbar的使用

  1. Toolbar的基础使用
  2. Toolbar配置主题Theme
  3. Toolbar中常用的控件设置
  4. Toolbar的自定义

Toolbar的基础使用

我们从以下几点来一步一步的学习Toolbar的使用

  1. Style(风格)
  2. Layout(布局)
  3. Activity(代码)

Style

为了能在你的Activity中使用Toolbar,你必须在工程里修改styles.xml文件里的主题风格,系统默认如下

  1. <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

这种Theme表示使用系统之前的ActionBar,那么我们想要使用Toolbar怎么办呢?

  1. <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

这个主题表示不使用系统的Actionbar了,这是第一步。

Layout布局

为了使用Toolbar,我们第二步是在你的activity_main.xml布局里添加Support v7提供的Toolbar,代码如下

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".MainActivity">
  6. <android.support.v7.widget.Toolbar
  7. android:id="@+id/toolbar"
  8. android:layout_width="match_parent"
  9. android:layout_height="?attr/actionBarSize"
  10. android:minHeight="?attr/actionBarSize"
  11. >
  12. </android.support.v7.widget.Toolbar>
  13. </RelativeLayout>

为了在你的UI中使用Toolbar,你得为每个activity布局添加Toolbar,并且给Toolbar设置一个id android:id=”@+id/toolbar”。这是第二部。

代码添加Toolbar

为了使用Toolbar,我们第三部是在你的Activity代码中做如下代码处理:

  1. Toolbar toolbar;
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. toolbar = findView(R.id.toolbar);
  7. setSupportActionBar(toolbar);
  8. }

代码中通过findView找到Toolbar,然后通过setSupportActionBar(toolbar);将Toolbar设置为Activity的导航栏。

通过上面的三个步骤,你就已经使用了Support v7提供的Toolbar了。看看那效果图。

 
是不是感觉很丑?没有一点MD设计的风格,不过别失望,这仅仅是为了让Toolbar正常工作而已,为了让Toolbar有Material Design风格,我们必须去设置Toolbar的主题风格。

Toolbar配置主题Theme

我们重新配置系统主题Theme,修改styles.xml代码如下:

  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>

各个属性就不解释了,注释都很清楚。我们来看看Toolbar怎么使用这些主题吧?

配置activity_main.xml中的Toolbar改成为如下:

  1. <android.support.v7.widget.Toolbar
  2. android:id="@+id/toolbar"
  3. android:layout_width="match_parent"
  4. android:layout_height="?attr/actionBarSize"
  5. android:background="?attr/colorPrimary"
  6. >
  7. </android.support.v7.widget.Toolbar>

相比上面的Toolbar配置,这里只多添加了 这么一行代码

  1. android:background="?attr/colorPrimary"

给Toolbar设置背景属性,这里使用了styles.xml文件中如下属性

  1. <!--导航栏底色-->
  2. <item name="colorPrimary">@color/accent_material_dark</item>

经过如下配置再来看看效果图吧!

效果有点改进,我们继续发现Toolbar的优势吧!

Toolbar中常用的控件设置

那么Toolbar是否都有Actionbar的所有功能呢?毋庸置疑,来看代码:

  1. toolbar = findView(R.id.toolbar);
  2. setSupportActionBar(toolbar);
  3. getSupportActionBar().setDisplayShowTitleEnabled(false);
  4. toolbar.setTitle("主标题");
  5. toolbar.setSubtitle("副标题");
  6. toolbar.setLogo(R.drawable.ic_launcher);
  7. toolbar.setNavigationIcon(android.R.drawable.ic_input_delete);

Toolbar可以设置 Title(主标题),Subtitle(副标题),Logo(logo图标)NavigationIcon(导航按钮)。

注意 如果你想要通过toolbar.setTitle(“主标题”);设置Toolbar的标题,你必须在调用它之前调用如下代码:

  1. getSupportActionBar().setDisplayShowTitleEnabled(false);

上面代码用来隐藏系统默认的Title。

那么Toolbar能不能使用Menu菜单功能呢?答案是肯定的了。来看看加载如下menu菜单的Toolbar吧

  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_edit"
  7. android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
  8. android:orderInCategory="80"
  9. android:title="查找"
  10. app:showAsAction="always" />
  11. <item
  12. android:id="@+id/action_share"
  13. android:icon="@drawable/abc_ic_menu_share_mtrl_alpha"
  14. android:orderInCategory="90"
  15. android:title="分享"
  16. app:showAsAction="always" />
  17. <item
  18. android:id="@+id/action_settings"
  19. android:orderInCategory="100"
  20. android:title="@string/action_settings"
  21. app:showAsAction="never" />
  22. </menu>

怎么给menu的各个Item添加点击事件呢?Toolbar给我们提供如下方法

Activity继承Toolbar的OnMenuItemClickListener接口

  1. public class MainActivity extends AppCompatActivity implements Toolbar.OnMenuItemClickListener
  1. //实现接口
  2. toolbar.setOnMenuItemClickListener(this);
  3. @Override
  4. public boolean onMenuItemClick(MenuItem item) {
  5. switch (item.getItemId()) {
  6. case R.id.action_edit:
  7. Toast.makeText(this, "查找按钮", Toast.LENGTH_SHORT).show();
  8. break;
  9. case R.id.action_share:
  10. Toast.makeText(this, "分享按钮", Toast.LENGTH_SHORT).show();
  11. break;
  12. }
  13. return false;
  14. }

至此,Toolbar添加控件就基本完结了,来看看效果如下

是不是很炫?我们还没有使用自定义的Toolbar呢?那怎么使用呢?

Toolbar的自定义

其实Toolbar是继承ViewGroup的一个容器控件,言外之意就是我们可以在Toolbar添加自己的布局了。看代码

  1. <android.support.v7.widget.Toolbar
  2. android:id="@+id/toolbar"
  3. android:layout_width="match_parent"
  4. android:layout_height="?attr/actionBarSize"
  5. android:background="?attr/colorPrimary">
  6. <RelativeLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:gravity="center">
  10. <Button
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="add" />
  14. <TextView
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:layout_centerInParent="true"
  18. android:layout_gravity="center_vertical"
  19. android:text="标题"
  20. android:textSize="18sp" />
  21. <ImageView
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_alignParentRight="true"
  25. android:background="@drawable/abc_ic_menu_share_mtrl_alpha" />
  26. </RelativeLayout>
  27. </android.support.v7.widget.Toolbar>

效果图: 

这样我们就可以任意给Toolbar布局了。也解决了标题不能居中的问题。有特殊需求的Toolbar的童鞋就可以自行补脑实现各种需求效果啦!

Android5.x Material Design 主题风格Theme配置

在通往Material Design风格的路上总是遥远的,但也阻挡不了我们学习的劲头,仅仅会使用Toolbar是不够的。除了Toolbar的风格,我们还可以通过设置Theme主题该控制Android很多控件的风格。直接上一张图片效果。

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

  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输入框字体的颜色。

Android5.x新特性之 Toolbar和Theme的使用的更多相关文章

  1. Android群英传笔记——第十二章:Android5.X 新特性详解,Material Design UI的新体验

    Android群英传笔记--第十二章:Android5.X 新特性详解,Material Design UI的新体验 第十一章为什么不写,因为我很早之前就已经写过了,有需要的可以去看 Android高 ...

  2. Android5.0新特性-Material Design

    概述 2014年,Google携Android5.X重装归来.全新的UI设计和更加优化的性能,令开发人员眼前一亮 安装和配置Android5.0开发环境 开发Android还得靠AS.下载地址 htt ...

  3. Android5.0新特性——兼容性(support)

    兼容性 虽然Material Design新增了许多新特性,但是并不是所有新内容对对下保持了兼容. 使用v7包 v7 support libraries r21 及更高版本包含了以下Material ...

  4. Android5.0新特性之——按钮点击效果动画(涟漪效果)

    Android5.0 Material Design设计的动画效果 RippleDrawable涟漪效果 涟漪效果是Android5.0以后的新特性.为了兼容性,建议新建drawable-v21文件夹 ...

  5. Android5.0新特性——新增的Widget(Widget)

    新增的Widget RecyclerView RecyclerView是ListView的升级版,它具备了更好的性能,且更容易使用.和ListView一样,RecyclerView是用来显示大量数据的 ...

  6. Android5.0新特性——全新的动画(animation)

    全新的动画 在Material Design设计中,为用户与app交互反馈他们的动作行为和提供了视觉上的连贯性.Material主题为控件和Activity的过渡提供了一些默认的动画,在android ...

  7. Android5.0新特性——Material Design简介

    Material Design Material Design简介 Material Design是谷歌新的设计语言,谷歌希望寄由此来统一各种平台上的用户体验,Material Design的特点是干 ...

  8. Android5.0新特性:RecyclerView实现上拉加载更多

    RecyclerView是Android5.0以后推出的新控件,相比于ListView可定制性更大,大有取代ListView之势.下面这篇博客主要来实现RecyclerView的上拉加载更多功能. 基 ...

  9. Android5.0新特性——阴影和剪裁(shadow)

    阴影和剪裁 View的z属性 Material Design建议为了凸显布局的层次,建议使用阴影效果,并且Android L为了简化大家的工作,对View进行了扩展,能使大家非常方便的创建阴影效果: ...

随机推荐

  1. 阐述:SIP协议是什么

    sip协议是什么?可能刚刚接触这个协议的朋友会掌握不好它的定义.那么首先我们要了解一下,目前企业中大多数VoIP应用都使用H.323协议,但是,随着越来越多的企业研究SIP协议,不久的将来基于SIP协 ...

  2. Linux操作_常用命令操作练习

    1,新键一个用户,该用户名为自己姓名首字母缩写+学号最后2位组成(如王东,学号最后2位为18,则该用户名为wd18),为该用户设置密码,并将其加到users组:将该用户的相关信息更改(要求:Name为 ...

  3. (转)WAVE PCM 声音文件格式

    WAVE文件格式是Microsoft为存储多媒体的RIFF规范的一部分.一个RIFF文件以一个文件头开始,然后是一系列的数据块.一个WAVE文件常常仅由一个WAVE块构成,WAVE块包含一个说明格式的 ...

  4. SQLException: Column count doesn't match value count at row 1

    INSERT INTO table_name(col_name1, col_name2, col_name3) VALUES('value1','value2'); 语句中,前后列数不等造成的 转自: ...

  5. WAS启动报错Service failed to start. startServer return code = -1

    http://www-01.ibm.com/support/docview.wss?uid=swg21368020 Problem(Abstract) Attempts to start IBM We ...

  6. python3 post方式上传文件。

    借助第三方库:Requests 其官网地址:   http://python-requests.org       官网上写的安装方式:http://docs.python-requests.org/ ...

  7. S 参数说明

     微波系统主要研究信号和能量两大问题:信号问题主要是研究幅频和相频特性:能量问题主要是研究能量如何有效地传输.微波系统是分布参数电路,必须采用场分析法,但场分析法过于复杂,因此需要一种简化的分析方法. ...

  8. 第一個shell腳本

    #!/bin/bash echo "Hello World !" 運行

  9. Linux-centos6.8下关闭防火墙

    一.临时关闭防火墙 1. 查看防火墙的状态 [root@vpnSS ~]# /etc/init.d/iptables status Table: filter Chain INPUT (policy ...

  10. linux下redis的安装和集群搭建

    一.redis概述 1.1.目前redis支持的cluster特性: 1):节点自动发现. 2):slave->master 选举,集群容错. 3):Hot resharding:在线分片. 4 ...