toolbar是android sdk API21新增的组件,下面是谷歌官方的介绍文档:

A standard toolbar for use within application content.

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 setActionBar() method.

Toolbar supports a more focused feature set than ActionBar. From start to end, a toolbar may contain a combination of the following optional elements:

  • A navigation button. This may be an Up arrow, navigation menu toggle, close, collapse, done or another glyph of the app's choosing. This button should always be used to access other navigational destinations within the
    container of the Toolbar and its signified content or otherwise leave the current context signified by the Toolbar. The navigation button is vertically aligned within the Toolbar's minimum
    height
    , if set.
  • A branded logo image. This may extend to the height of the bar and can be arbitrarily wide.
  • A title and subtitle. The title should be a signpost for the Toolbar's current position in the navigation hierarchy and the content contained there. The subtitle, if present should indicate any extended information about
    the current content. If an app uses a logo image it should strongly consider omitting a title and subtitle.
  • One or more custom views. The application may add arbitrary child views to the Toolbar. They will appear at this position within the layout. If a child view'sToolbar.LayoutParams indicates
    Gravity value
    of CENTER_HORIZONTAL the
    view will attempt to center within the available space remaining in the Toolbar after all other elements have been measured.
  • An action
    menu
    .
     The menu of actions will pin to the end of the Toolbar offering a few frequent,
    important or typical
     actions along with an optional overflow menu for additional actions. Action buttons are vertically aligned within the Toolbar's minimum
    height
    , if set.

In modern Android UIs developers should lean more on a visually distinct color scheme for toolbars than on their application icon. The use of application icon plus title as a standard layout is discouraged on API 21 devices and newer.

上面的介绍的重点是,toolbar是api21 新增的组件,其主要用来弥补actionbar带来的不足,其继承自viewgroup,自由的属性包括导航按钮(类似向上按钮),logo图片,标题和子标题,一个或者多个自定义view,菜单。

其中有一句介绍尤为介绍 An application may choose to designate a Toolbar as the action bar for an Activity using the setActionBar() method.我们可以在任何activity中使用toolbar作为actiobar,但是api21之前只能使用setSupportActionbar(),相应的主题也要设置为NoActionbar(不要怀疑就是这样干,没错儿),about
toolbar的好,我会娓娓道来(么么哒

具体介绍详见sdk/docs/reference/android/widget/Toolbar.html

下面介绍一下AppCompatAcitivity

官方介绍:You can add an ActionBar to
your activity when running on API level 7 or higher by extending this class for your activity and setting the activity theme toTheme.AppCompat or
a similar theme.

其主要用来向低版本的sdk提供添加actionbar的功能,并且提供Theme.AppCompat(就是谷歌推荐的Theme.Material)这样的主题,保证低版本也能获得高版本的效果。

详细介绍见:yourSDKpath/sdk/docs/reference/android/support/v7/app/AppCompatActivity.html

谷歌在新出的22.1 library 还包含了如下的组件,他们最大的特点就是可以在组件之中使用android:theme:来控制组件的样式

AppCompatAutoCompleteTextView 

AppCompatButton 

AppCompatCheckBox 

AppCompatCheckedTextView 

AppCompatEditText 

AppCompatMultiAutoCompleteTextView 

AppCompatRadioButton 

AppCompatRatingBar 

AppCompatSpinner 

AppCompatTextView

在介绍了如此多的好东西之后,我们开始步入正题,如何使用toolbar 与AppCompatAcitivity实现浸入式Statusbar效果。

首先使用AndroidStudio构建project。

第二步添加依赖库:

选择open module settings》app》dependency,点击下方的加号自行选择    'com.android.support:appcompat-v7:22.1.1'

第三步:修改系统自带主题

将原有的values /style修改为:

[html] view
plain
copy

  1. <pre name="code" class="html"><resources>
  2. <style name="ActionBarTheme" parent="Theme.AppCompat.NoActionBar">
  3. <item name="windowActionBar">false</item>
  4. <item name="android:windowNoTitle">true</item>
  5. <!-- toolbar(actionbar)颜色 -->
  6. <item name="colorPrimary">#673AB7</item>
  7. <!-- 窗口的背景颜色 -->
  8. <item name="colorPrimaryDark">@android:color/holo_blue_bright</item>
  9. </style>
  10. ;/resources>

之所以这样修改是为了能够使用toolbar,所以我们需要将主题设置为

[html] view
plain
copy

  1. Theme.AppCompat.NoActionBar否则会因冲突造成程序crash。

然后我们在相同目录下新建一个folder named:values-v19,此包的作用是提供在api19+以上的效果

我们在其中复制style文件并修改:

[html] view
plain
copy

  1. <resources>
  2. <style name="ActionBarTheme" parent="Theme.AppCompat.NoActionBar">
  3. <item name="windowActionBar">false</item>
  4. <item name="android:windowNoTitle">true</item>
  5. <!-- toolbar(actionbar)颜色 -->
  6. <item name="colorPrimary">#673AB7</item>
  7. <!-- 状态栏颜色 -->
  8. <item name="colorPrimaryDark">@android:color/holo_blue_bright</item>
  9. </style>
  10. </resources>

其中的状态栏颜色只会在api19 以上才会生效。

第四步创建布局文件:

[html] view
plain
copy

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.v7.widget.Toolbar
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:id="@+id/toolbar"
  5. android:layout_width="match_parent"
  6. android:layout_height="wrap_content"
  7. android:background="@android:color/holo_blue_bright"
  8. android:theme="@style/ThemeOverlay.AppCompat.Dark"
  9. android:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
  10. android:minHeight="?attr/actionBarSize">
  11. </android.support.v7.widget.Toolbar>
[html] view
plain
copy

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. android:fitsSystemWindows="true">
  8. <include layout="@layout/toobar"></include>
  9. <android.support.v7.widget.AppCompatCheckedTextView
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. android:text="Hello World, MyActivity"/>
  13. </LinearLayout>

第五步:使用AppcompatAcitivity与toolbar

[java] view
plain
copy

  1. public class MainActivity extends AppCompatActivity {
  2. private Toolbar mToolbar;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. //        this.getWindow().setBackgroundDrawableResource(getResources().getColor(android.R.color.holo_blue_bright));
  8. //        设定状态栏的颜色,当版本大于4.4时起作用
  9. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  10. SystemBarTintManager tintManager = new SystemBarTintManager(this);
  11. tintManager.setStatusBarTintEnabled(true);
  12. tintManager.setStatusBarTintResource(android.R.color.holo_blue_bright);
  13. }
  14. mToolbar = (Toolbar) findViewById(R.id.toolbar);
  15. mToolbar.setTitle("主标题");// 标题的文字需在setSupportActionBar之前,不然会无效
  16. setSupportActionBar(mToolbar);
  17. }
  18. }

这样的话我们就可以实现了浸入式的效果,在项目中我使用了一个开源的工程SystemBarTintManager,这个资源会在我会贴在源码之中。

然后贴出运行效果:

诸位可能以为我们的追求到此就结束了,其实还未完成。对于这个toolbar我们还未把玩一番,看到其继承自viewgroup,于是我好奇在其中添加了一个子view,发现竟然能够运行成功

[html] view
plain
copy

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.v7.widget.Toolbar
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:id="@+id/toolbar"
  5. android:layout_width="match_parent"
  6. android:layout_height="wrap_content"
  7. android:background="@android:color/holo_blue_bright"
  8. android:theme="@style/ThemeOverlay.AppCompat.Dark"
  9. android:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
  10. android:minHeight="?attr/actionBarSize">
  11. <TextView
  12. android:layout_width="match_parent"
  13. android:layout_height="40dp"
  14. android:text="123"/>
  15. </android.support.v7.widget.Toolbar>

标题栏居然显示了123,之前设置title就此失效了,之后我又放入一个新的LinearLayout,也能正确布局,如果有子布局会使得setTitile方法失效,而  mToolbar.setNavigationIcon方法却不受其影响。

如果我们想要模拟actionbar上面的导航按钮类似向上这样的效果,需要在java代码中进行配置mToolbar.setNavigationIcon,xml会因为api版本问题得不到解决。如果不想使用actionbar,我们可以将toolbar注释掉,状态栏的颜色一样是可控的。么么哒,转载请注明出处

ToolBar与AppcompatAcitivity实现浸入式Statusbar效果的更多相关文章

  1. 手机端的tab切换,响应式切换效果

    之前写过这些tab切换的效果,无论网页上还是手机端,网上也有很多的例子,这个好像是我参考网上,也不知道是哪里的了.总结了一下,就当保存下来了把. <!DOCTYPE html > < ...

  2. Android中的沉浸式状态栏效果

    无意间了解到沉浸式状态栏,感觉贼拉的高大上,于是就是试着去了解一下,就有了这篇文章.下面就来了解一下啥叫沉浸式状态栏.传统的手机状态栏是呈现出黑色条状的,有的和手机主界面有很明显的区别.这一样就在一定 ...

  3. Toolbar融入状态栏实现沉浸式遇到的问题

    这里写一个纠结我一下午的问题,目的是写一个toolbar和状态栏相融合的沉浸式的样子,遇到各种各样的问题,还好最后慢慢解决了. 一.首先在活动中将状态栏设为透明 @Override protected ...

  4. js原生实现链式动画效果

    // 1. css样式 div { width: 100px; height: 100px; background: olivedrab; position: absolute; left: 0px; ...

  5. 【android开发笔记】为Button的背景图片添加边框式样式效果

    现在做的项目遇到一个问题,设计给过来的图片只有一种状态,但是实现的需求是要求有两个状态,另一种选状态为图片背景加边框.如图: 刚开使用使用ImageView ,ImageViewButton 效果不是 ...

  6. SQL-Oracle内实现柱形图式的效果

    在SQL SERVER内有一个函数replicate()可以实现柱形图效果,本质上是利用字符重复出现的次数来控制柱形图的长短,效果如图: 如果要在Oracle内实现相同的效果,则需要自己写一个函数: ...

  7. 微信小程序之实现页面缩放式侧滑效果

    效果图: 实现原理:点击按钮,往需要动画的div中添加或移除拥有动画效果的class. 由于微信小程序中不能操作page这个根节点,所以,只有用一个div(view)来模仿page根节点. 1.结构 ...

  8. android 4.4以上能够实现的沉浸式状态栏效果

    仅仅有android4.4以及以上的版本号才支持状态栏沉浸效果 先把程序执行在4.4下面的手机上,看下效果: 在4.4以上的效果: watermark/2/text/aHR0cDovL2Jsb2cuY ...

  9. css实现响应式九宫格效果

    1. 首先看下九宫格的效果图: 2. html代码比较简单,如下: <div class="main"> <div class="box1"& ...

随机推荐

  1. ●BZOJ 2693 jzptab

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2693 题解: 莫比乌斯反演 先看看这个题,BZOJ 2154 Crash的数字表格,本题的升 ...

  2. 2015 多校联赛 ——HDU5299(树删边)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission ...

  3. 例10-7 uva10820(欧拉)

    题意:输入n,要求满足1≤x,y≤n,且x,y互素的个数. 若输入2,则答案3为(1,1),(1,2),(2,1);所以欧拉函数求出所有数的phi值,除了1之外都加上phi值的2倍即可 通过推导: p ...

  4. python3 字符串str 教程

    字符串可以用单引号或双引号来创建. Python 不支持单字符类型,单字符也在Python也是作为一个字符串使用. 例: var1 = 'Hello World!' var2 = "Pyth ...

  5. Vue2学习(3)

    子组件索引 尽管有 props 和 events,但是有时仍然需要在 JavaScript 中直接访问子组件.为此可以使用 ref 为子组件指定一个索引 ID.例如: <div id=" ...

  6. 百度ML/DL方向面经

    最近败人品败得有些厉害,很多事都处理得不好--感觉有必要做点好事攒一攒. 虽然可能面试经过不是很有代表性,不过参考价值大概还是有的-- 由于当时人在国外,三轮都是电面-- 一面 当地时间早上5点半爬起 ...

  7. python2.7入门---内置函数

        内置函数     abs() divmod() input() open() staticmethod() all() enumerate() int() ord() str() any() ...

  8. SSH(struts2+hibernate+spring)总结

    1 前三个文章 是我对ssh的具体实现 虽然没有真的写一个ssh的例子出来 但是 意思应该传达到了 主要还是注解注入的ssh太模块化了 感觉写出来意义不大 个人水平有限 说不清 2 我一开是写的是st ...

  9. python笔记二(数据类型和变量、编码方式、字符串的编码、字符串的格式化)

    一.数据类型 python可以直接处理的数据类型有:整数.浮点数.字符串.布尔值.空值. 整数 浮点数 字符串:双引号内嵌套单引号,可以输出 i'm ok. 也可以用\来实现,\n 换行 \t tab ...

  10. 实验与作业(Python)-04 数据类型、数学函数与字符串

    截止日期 实验目标 继续熟悉for循环与turtle 数值运算符 math库常用函数 字符串转化为数值类型(int, float, complex) 字符串常用函数 实验内容 任务1.使用for代替w ...