ActionBar 简介

视频为本篇播客知识点讲解,建议采用超清模式观看, 欢迎点击订阅我的优酷

讲解ToolBar之前首先需要了解 ActionBar, 两者使用起来基本上一致。

Android 3.0 Android 推了 ActionBar 这个控件,而到了2013 年 Google 开始大力地推动所谓的 android style,想要逐渐改善过去 android 纷乱的界面设计,希望让终端使用者尽可能在 android 手机有个一致的操作体验。

先来看一眼ActionBar的样子

ActionBar里面包含logo,标题, 菜单等等,怎么给应用程序添加ActionBar呢?

ActionBar存在两个不同包下的类, 分别为兼容低版本的(android.support.v7.app.ActionBar),和不兼容低版本的(android.app.ActionBar)。使用起来基本上没有区别, 一般情况都是采用兼容低版本的,所有大家一定不要导错包。

使用起来首先修改应用程序主题为包含ActionBar的, 然后让Activity继承AppCompatActivity, 这样就可以看到ActionBar了

    <!--主题-->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

通过getSupportActionBar(); 就可以获取兼容低版本的ActionBar, 然后就可以进行一系列设置。比如设置标题 setTiTle 之类的。

ActionBar 创建菜单

视频为本篇播客知识点讲解,建议采用超清模式观看, 欢迎点击订阅我的优酷

Android新版本 菜单键默认已经不弹出菜单了,而是弹出最近任务列表, 菜单列表集成到了ActionBar的右上角, 可以隐藏到列表里, 也可以显示到外面。

通过onCreateOptionsMenu 方法加载菜单

    public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

通过onOptionsItemSelected 处理菜单的点击事件

  @Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId(); if (id == R.id.action_settings) {
Toast.makeText(this,"我是设置按钮",Toast.LENGTH_SHORT).show();
return true;
}
if(id==R.id.action_add){
Toast.makeText(this,"我是添加按钮",Toast.LENGTH_SHORT).show();
return true;
}
if(id==android.R.id.home){
Toast.makeText(this,"home",Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}

再来看看菜单文件是怎么定义的

<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:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/> <!--orderInCategory显示优先级 orderInCategory值越大优先级越低 -->
<item
android:id="@+id/action_add"
android:orderInCategory="100"
android:title="你好"
android:icon="@mipmap/ic_launcher"
app:showAsAction="ifRoom|withText"/>
<item
android:id="@+id/action_add2"
android:orderInCategory="100"
android:title="你好"
android:icon="@mipmap/ic_launcher"
app:showAsAction="always|withText"/>
</menu>

有几个属性,需要重点介绍下,

title菜单标题,

icon菜单图标,

orderInCategory显示的优先 级,

showAsAction 显示的方式, 其中never表示从不显示到外面, always一直显示到ActionBar上面,ifRoom如果ActionBar空间足够显示到上面, 空间不够隐藏到 overflow里面(就是上面三个点的图片 点击弹出的列表中)

Toolbar

视频为本篇播客知识点讲解,建议采用超清模式观看, 欢迎点击订阅我的优酷

介绍完了ActionBar 我们再来看看Toolbar的设计理念

Android 5.0 推出material design(材料设计),官方在某些程度上认为 ActionBar 限制了 android app 的开发与设计的弹性。

Toolbar其实就是为了取代ActionBar, 因为ActionBar 局限性太大, ToolBar更利于扩展, 使用方法很相似

如何用Toolbar 取代ActionBar呢 ,

1.去掉ActionBar

首先第一步更改主题, 选择NoActionBar ,这样就保证当前应用程序不存在ActionBar了

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

2.布局文件中定义ToolBar

 <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
/>

3.代码中设置ToolBar取代ActionBar

  @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_bar);
Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar); // 用ToolBar代替ActionBar }

Toolbar 可以用来做什么呢

使用起来和ActionBar非常相似, 菜单添加都是一模一样

   toolbar.setTitle("我是标题");
toolbar.setSubtitle("子标题");
toolbar.setLogo(R.mipmap.ic_launcher);
toolbar.setNavigationIcon(R.mipmap.ic_launcher);

我们可能也许会有需求就是让Toolbar显示的标题居中,这也不难, Toolbar非常灵活,可以在里面添加TextView, 但是还需要把toolbar的标题设置为空,否则的话显示的太难看了。

 toolbar.setTitle(" ");

 <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
>
<!--gravity 文字在控件中居中
layout_gravity 控件在父容器的位置
-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:layout_centerInParent="true"
android:text="我是标题"
android:textSize="20sp"/> </android.support.v7.widget.Toolbar>

主题设置

视频为本篇播客知识点讲解,建议采用超清模式观看, 欢迎点击订阅我的优酷

Toolbar可以单独设置主题,也可以单独添加菜单颜色主题

  <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:theme="" // Toolbar主题
android:popupTheme="" // 菜单主题控制
....
> </android.support.v7.widget.Toolbar>

如果你不单独给Toolbar设置主题,toolbar就会采用系统默认主题

我们是可以给系统主题添加一些条目,控制一些组件的颜色,如下面的主题就设置了三种颜色,根据不同的name会自动控制不同区域的颜色

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

具体分别控制什么颜色呢?

  1. android:colorPrimaryDark 应用的主要暗色调,statusBarColor默认使用该颜色
  2. android:statusBarColor 状态栏颜色,默认使用colorPrimaryDark
  3. android:colorPrimary 应用的主要色调,actionBar默认使用该颜色
  4. android:windowBackground 窗口背景颜色
  5. android:navigationBarColor 底部栏颜色
  6. android:colorForeground 应用的前景色,ListView的分割线,switch滑动区默认使用该颜色
  7. android:colorBackground 应用的背景色,popMenu的背景默认使用该颜色
  8. android:colorAccent 一般控件的选种效果默认采用该颜色
  9. android:colorControlNormal 控件的默认色调 
  10. android:colorControlHighlight 控件按压时的色调
  11. android:colorControlActivated 控件选中时的颜色,默认使用colorAccent
  12. android:colorButtonNormal 默认按钮的背景颜色
  13. android:textColor Button,textView的文字颜色
  14. android:textColorPrimaryDisableOnly RadioButton checkbox等控件的文字
  15. android:textColorPrimary 应用的主要文字颜色,actionBar的标题文字默认使用该颜色

后期补充

其实主题还可以做很多操作,不仅仅可以控制主题颜色,还可以控制动画之类的,总之很强大 ,

windowBackground 背景

windowBackgroundFallback

windowClipToOutline

windowFrame Dialog 是否有边框

windowNoTitle 是否有标题

windowFullscreen 是否为全屏

windowOverscan 是否要求窗体铺满整屏幕

windowIsFloating 是否浮在下层之上

windowContentOverlay 设置覆盖内容背景

windowShowWallpaper 是否显示壁纸

windowTitleStyle 标题栏Style

windowTitleSize 窗体文字大小

windowTitleBackgroundStyle 标题栏背景style

windowAnimationStyle 切换时的动画样式

windowSoftInputMode 在使用输入法时窗体的适配

windowActionBar 是否打开ActionBar

windowActionModeOverlay 是否覆盖action

windowCloseOnTouchOutside 是否再点击外部可关闭

windowTranslucentStatus 是否半透明状态

windowTranslucentNavigation 是否使用半透明导航

windowDrawsSystemBarBackgrounds 是否绘制系统导航栏背景

statusBarColor 状态栏颜色

navigationBarColor 导航栏颜色

windowActionBarFullscreenDecorLayout 全屏时的布局

windowContentTransitions 内容是否转换

windowActivityTransitions 活动时候转换

本章教程就讲解到这了,欢迎点击订阅我的优酷

Android教程 -08 ToolBar的使用和主题的介绍的更多相关文章

  1. android 5.X Toolbar+DrawerLayout实现抽屉菜单

    前言  android5.X新增的一个控件Toolbar,这个控件比ActionBar更加自由,可控,因为曾经的ActionBar的灵活性比較差,所以google逐渐使用Toolbar替代Action ...

  2. Android——MaterialDesign之一Toolbar

    Toolbar 由于ActionBar设计原因只能存在活动的顶部,从而不能实现MaterialDesign的效果,现在推荐使用Toolbar,继承Actionbar,但是比起它更加的灵活. 设置主题: ...

  3. Xamarin Android教程如何使用Xamarin开发Android应用

    Xamarin Android教程如何使用Xamarin开发Android应用 在了解了Xamarin和Andriod系统之后,下面我们需要了解一下如何使用这些工具和系统来开发我们的应用程序. And ...

  4. Xamarin Android教程Android基本知识版本介绍与系统介绍

    Xamarin Android教程Android基本知识版本介绍与系统介绍 Xamarin Android教程Android基本知识版本介绍与系统介绍,开发Andriod有时候不像iOS一样轻松,因为 ...

  5. Android API 21 Toolbar Padding

    up vote117down votefavorite 44 How do I get rid of the extra padding in the new Toolbar with Android ...

  6. Android应用开发中的风格和主题(style,themes)

    http://www.cnblogs.com/playing/archive/2011/04/01/2002469.html 越来越多互联网企业都在Android平台上部署其客户端,为了提升用户体验, ...

  7. Android动态修改ToolBar的Menu菜单

    Android动态修改ToolBar的Menu菜单 效果图 实现 实现很简单,就是一个具有3个Action的Menu,在我们滑动到不同状态的时候,把对应的Action隐藏了. 开始上货 Menu Me ...

  8. Android Spinner In Toolbar

    As the title of the post suggest in this tutorial we will see how to have spinner widget inside the ...

  9. [译]Vulkan教程(08)逻辑设备和队列

    [译]Vulkan教程(08)逻辑设备和队列 Introduction 入门 After selecting a physical device to use we need to set up a  ...

随机推荐

  1. js中this指向学习总结

      在面向对象的语言中(例如Java,C#等),this 含义是明确且具体的,即指向当前对象.一般在编译期绑定. 然而js中this 是在运行期进行绑定的,这是js中this 关键字具备多重含义的本质 ...

  2. linux系统RabbitMQ启动错误记录

    安装并配置好RabbitMq之后终端执行rabbitmq-server报错 试了网上的各种方法也无济于事 最后发现可能是因为访问权限的问题(并不确定) 解决方法:sudo rabbitmq-serve ...

  3. 2017年浙工大迎新赛热身赛 J Forever97与寄信 【数论/素数/Codeforces Round #382 (Div. 2) D. Taxes】

    时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 131072K,其他语言262144K64bit IO Format: %lld 题目描述 Forever97与未央是一对笔友,他们经常互 ...

  4. IDEA maven项目查自动查看依赖关系,解决包冲突问题

    在maven项目中找到pom.xml,打开. <dependencies> <dependency> <groupId>org.apache.storm</g ...

  5. JMeter 参数意义

    样本数目:运行时得到的取样器响应结果个数 最新样本:最近一个取样器结果的响应时间 平均:所有取样器结果的响应时间平均值 偏离:所有取样器结果的响应时间标准差 吞吐量:每分钟响应的取样器结果个数 中值: ...

  6. day39-Spring 19-今天的内容总结

  7. More Effective C++: 04效率

    16:牢记80-20准则 80-20准则说的是大约20%的代码使用了80%的程序资源:大约20%的代码耗用了大约80%的运行时间:大约20%的代码使用了80%的内存:大约20%的代码执行80%的磁盘访 ...

  8. 为数据计算提供强力引擎,阿里云文件存储HDFS v1.0公测发布

    在2019年3月的北京云栖峰会上,阿里云正式推出全球首个云原生HDFS存储服务—文件存储HDFS,为数据分析业务在云上提供可线性扩展的吞吐能力和免运维的快速弹性伸缩能力,降低用户TCO.阿里云文件存储 ...

  9. 2016中国银行Top100榜单发布 工行排首位

    2016中国银行Top100榜单发布 工行排首位 2016-07-09 15:13:19 第一财经   2016年7月8日,中国银行业协会在京召开“<中国银行业发展报告(2016)>发布会 ...

  10. nodeJs学习-13 router

    const express=require('express'); var server=express(); //目录1:/user/ var routeUser=express.Router(); ...