从实际使用须要出发,以最简单的方式实现了几种类型的MD状态栏。

(重点在fitsSystemWindows的使用)

0,使用前提

Theme.AppCompat.Light.DarkActionBar

targetSdkVersion 23;

support librariy 23.3.0

styles-v19: <item name="android:windowTranslucentStatus">true</item>

styles-v21+: <item name="android:windowDrawsSystemBarBackgrounds">true</item>  <item name="android:statusBarColor">@android:color/transparent</item>

1。最普通的类型:仅仅有一个ToolBar

layout:

  1. <android.support.design.widget.CoordinatorLayout>
  2.  
  3. <android.support.design.widget.AppBarLayout>
  4.  
  5. <android.support.v7.widget.Toolbar
  6. android:layout_height="wrap_content"
  7. android:fitsSystemWindows="true"/>
  8.  
  9. </android.support.design.widget.AppBarLayout>
  10.  
  11. </android.support.design.widget.CoordinatorLayout>

.java:

  1. /**
  2. * 简单型状态栏(ToolBar)
  3. *
  4. * @param activity
  5. */
  6. public static void setOrdinaryToolBar(Activity activity) {
  7. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  8. activity.getWindow().setStatusBarColor(ContextCompat.getColor(activity, R.color.colorPrimaryDark));
  9. } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
  10. setKKStatusBar(activity, R.color.colorPrimaryDark);
  11. }
  12. }

效果:

左:Android4.4  右:Android6.0

2,图片全屏透明状态栏(图片位于状态栏以下)

layout:

  1. <android.support.design.widget.CoordinatorLayout>
  2.  
  3. <ImageView/>
  4.  
  5. <android.support.v7.widget.Toolbar
  6. android:layout_height="wrap_content"
  7. android:fitsSystemWindows="true"/>
  8.  
  9. </android.support.design.widget.CoordinatorLayout>

.java

  1. /**
  2.  * 图片全屏透明状态栏(图片位于状态栏以下)
  3.  *
  4.  * @param activity
  5.  */
  6. public static void setImageTransparent(Activity activity) {
  7.     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  8.         activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
  9.     }
  10. }

效果:

左:Android4.4  右:Android6.0

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

3,图片全屏半透明状态栏(图片位于状态栏以下)

layout:

  1. <android.support.design.widget.CoordinatorLayout>
  2.  
  3. <ImageView/>
  4.  
  5. <android.support.v7.widget.Toolbar
  6. android:layout_height="wrap_content"
  7. android:fitsSystemWindows="true"/>
  8.  
  9. </android.support.design.widget.CoordinatorLayout>

.java:

  1. /**
  2. * 图片全屏半透明状态栏(图片位于状态栏以下)
  3. *
  4. * @param activity
  5. */
  6. public static void setImageTranslucent(Activity activity) {
  7. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  8. activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
  9. activity.getWindow().setStatusBarColor(ContextCompat.getColor(activity, R.color.statusBar));
  10. } else {
  11. setKKStatusBar(activity, R.color.statusBar);
  12. }
  13. }

效果:

左:Android4.4  右:Android6.0

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

4,ToolBar+TabLayout状态栏(ToolBar可伸缩)

layout:

  1. <android.support.design.widget.CoordinatorLayout
  2. android:background="@color/colorPrimaryDark"
  3. android:fitsSystemWindows="true">
  4.  
  5. <android.support.design.widget.AppBarLayout>
  6.  
  7. <android.support.v7.widget.Toolbar
  8. android:layout_height="?attr/actionBarSize"
  9. android:background="@color/colorPrimary"
  10. app:layout_scrollFlags="scroll|enterAlways|snap"/>
  11.  
  12. <android.support.design.widget.TabLayout/>
  13.  
  14. </android.support.design.widget.AppBarLayout>
  15.  
  16. <android.support.v4.widget.NestedScrollView/>
  17.  
  18. </android.support.design.widget.CoordinatorLayout>

.java:

  1. /**
  2. * ToolBar+TabLayout状态栏(ToolBar可伸缩)
  3. *
  4. * @param activity
  5. */
  6. public static void setToolbarTabLayout(Activity activity) {
  7. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  8. activity.getWindow().setStatusBarColor(ContextCompat.getColor(activity, R.color.colorPrimaryDark));
  9. }
  10. }

效果:

Android4.4:

Android6.0:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

5。DrawerLayout+ToolBar+TabLayout状态栏(ToolBar可伸缩)

layout:

  1. <android.support.v4.widget.DrawerLayout
  2. android:fitsSystemWindows="true">
  3.  
  4. <android.support.design.widget.CoordinatorLayout
  5. android:background="@color/colorPrimary">
  6.  
  7. <android.support.design.widget.AppBarLayout>
  8.  
  9. <android.support.v7.widget.Toolbar
  10. android:layout_height="wrap_content"
  11. app:layout_scrollFlags="scroll|enterAlways|snap"/>
  12.  
  13. <android.support.design.widget.TabLayout/>
  14.  
  15. </android.support.design.widget.AppBarLayout>
  16.  
  17. <android.support.v4.widget.NestedScrollView
  18. android:background="@android:color/white"
  19. app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
  20.  
  21. </android.support.design.widget.CoordinatorLayout>
  22.  
  23. <android.support.design.widget.NavigationView
  24. android:fitsSystemWindows="true"/>
  25.  
  26. </android.support.v4.widget.DrawerLayout>

.java:

  1. /**
  2. * DrawerLayout+ToolBar+TabLayout状态栏(ToolBar可伸缩)
  3. *
  4. * @param activity
  5. * @param drawerLayout
  6. * @param coordinatorLayout
  7. */
  8. public static void setDrawerToolbarTabLayout(Activity activity, CoordinatorLayout coordinatorLayout) {
  9. if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
  10. ViewGroup contentLayout = (ViewGroup) activity.findViewById(android.R.id.content);
  11. contentLayout.getChildAt(0).setFitsSystemWindows(false);
  12. coordinatorLayout.setFitsSystemWindows(true);
  13. setKKStatusBar(activity, R.color.statusBar);
  14. }
  15. }

效果:

Android4.4:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

Android6.0:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

6,CollapsingToolbarLayout状态栏(可折叠图片)

layout:

  1. <android.support.design.widget.CoordinatorLayout
  2. android:fitsSystemWindows="true">
  3.  
  4. <android.support.design.widget.AppBarLayout
  5. android:fitsSystemWindows="true">
  6.  
  7. <android.support.design.widget.CollapsingToolbarLayout>
  8.  
  9. <ImageView
  10. android:fitsSystemWindows="true"/>
  11.  
  12. <android.support.v7.widget.Toolbar
  13. android:layout_height="?attr/actionBarSize"
  14. app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"/>
  15.  
  16. </android.support.design.widget.CollapsingToolbarLayout>
  17.  
  18. </android.support.design.widget.AppBarLayout>
  19.  
  20. <android.support.v4.widget.NestedScrollView
  21. app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
  22.  
  23. </android.support.design.widget.CoordinatorLayout>

.java:

  1. /**
  2. * CollapsingToolbarLayout状态栏(可折叠图片)
  3. *
  4. * @param activity
  5. * @param coordinatorLayout
  6. * @param appBarLayout
  7. * @param imageView
  8. * @param toolbar
  9. */
  10. public static void setCollapsingToolbar(Activity activity, CoordinatorLayout coordinatorLayout,
  11. AppBarLayout appBarLayout, ImageView imageView, Toolbar toolbar) {
  12. if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
  13. coordinatorLayout.setFitsSystemWindows(false);
  14. appBarLayout.setFitsSystemWindows(false);
  15. imageView.setFitsSystemWindows(false);
  16. toolbar.setFitsSystemWindows(true);
  17. CollapsingToolbarLayout.LayoutParams lp = (CollapsingToolbarLayout.LayoutParams) toolbar.getLayoutParams();
  18. lp.height = (int) (getStatusBarHeight(activity) +
  19. activity.getResources().getDimension(R.dimen.abc_action_bar_default_height_material));
  20. toolbar.setLayoutParams(lp);
  21. setKKStatusBar(activity, R.color.statusBar);
  22. setCollapsingToolbarStatus(appBarLayout);
  23. }
  24. }

效果:

Android4.4:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

Android6.0:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

7,DrawerLayout+ToolBar型状态栏

layout:

  1. <android.support.design.widget.CoordinatorLayout>
  2.  
  3. <android.support.design.widget.AppBarLayout>
  4.  
  5. <android.support.v7.widget.Toolbar
  6. android:layout_height="wrap_content"
  7. android:fitsSystemWindows="true"/>
  8.  
  9. </android.support.design.widget.AppBarLayout>
  10.  
  11. <include layout="@layout/content_main"/>
  12.  
  13. </android.support.design.widget.CoordinatorLayout>

.java:

  1. /**
  2. * DrawerLayout+ToolBar型状态栏
  3. *
  4. * @param activity
  5. */
  6. public static void setDrawerToolbar(Activity activity) {
  7. if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
  8. ViewGroup contentLayout = (ViewGroup) activity.findViewById(android.R.id.content);
  9. contentLayout.getChildAt(0).setFitsSystemWindows(false);
  10. setKKStatusBar(activity, R.color.statusBar);
  11. }
  12. }

其他:

  1. /**
  2. * Android4.4CollapsingToolbar折叠时statusBar显示和隐藏
  3. *
  4. * @param appBarLayout
  5. */
  6. private static void setCollapsingToolbarStatus(AppBarLayout appBarLayout) {
  7. ViewCompat.setAlpha(mStatusBarView, 1);
  8. appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
  9. @Override
  10. public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
  11. int maxScroll = appBarLayout.getTotalScrollRange();
  12. float percentage = (float) Math.abs(verticalOffset) / (float) maxScroll;
  13. ViewCompat.setAlpha(mStatusBarView, percentage);
  14. }
  15. });
  16. }
  17.  
  18. private static void setKKStatusBar(Activity activity, int statusBarColor) {
  19. ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
  20. mStatusBarView = contentView.getChildAt(0);
  21. //改变颜色时避免反复加入statusBarView
  22. if (mStatusBarView != null && mStatusBarView.getMeasuredHeight() == getStatusBarHeight(activity)) {
  23. mStatusBarView.setBackgroundColor(ContextCompat.getColor(activity, statusBarColor));
  24. return;
  25. }
  26. mStatusBarView = new View(activity);
  27. ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
  28. getStatusBarHeight(activity));
  29. mStatusBarView.setBackgroundColor(ContextCompat.getColor(activity, statusBarColor));
  30. contentView.addView(mStatusBarView, lp);
  31. }
  32.  
  33. private static int getStatusBarHeight(Context context) {
  34. int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
  35. return context.getResources().getDimensionPixelSize(resourceId);
  36. }

效果:

Android4.4:

Android6.0:

OK!

源代码地址:https://github.com/XYScience/MDStatusBar

更简单更全的material design状态栏的更多相关文章

  1. 采用ADM2483磁隔离器让RS485接口更简单更安全

    采用ADM2483磁隔离器让RS485接口更简单更安全 摘要:本文介绍RS485的特点及应用,指出了普通RS485接口易损坏的问题,针对存在的问题介绍了以ADM2483为核心的磁隔离解决方案. 关键词 ...

  2. TSQL:让监控分析更简单更高效

    1. 前言 阿里时序时空数据库TSDB最新推出TSQL,支持标准SQL的语法和函数.用户使用熟悉的SQL,不仅仅查询更简单易用,用户还可以利用SQL强大的功能,实现更加复杂的计算分析. 2. 为什么需 ...

  3. Anno 让微服务、混合编程更简单(Net love Java)

    在社区或者QQ群我们经常看到有人争辩编程语言的好坏,只要一提起这个话题常常就能引来很多人参与,往往最后就变成了一群人几个小时的骂战.今天我们要说的是如何让Java和.Net(甚至更多语言)相结合.充分 ...

  4. [翻译]Kafka Streams简介: 让流处理变得更简单

    Introducing Kafka Streams: Stream Processing Made Simple 这是Jay Kreps在三月写的一篇文章,用来介绍Kafka Streams.当时Ka ...

  5. Kafka Streams简介: 让流处理变得更简单

    Introducing Kafka Streams: Stream Processing Made Simple 这是Jay Kreps在三月写的一篇文章,用来介绍Kafka Streams.当时Ka ...

  6. 走着官方的教程入门Material Design(一)

    又到期末了,学习下Google的材料设计.写下此文记录自己的同时,分享给需要的同学,若发现文中有什么问题和不对,欢迎指出 使用 Material Design 创建新应用 首先需要使用材料主题 如果是 ...

  7. 【知识必备】一文让你搞懂design设计的CoordinatorLayout和AppbarLayout联动,让Design设计更简单~

    一.写在前面 其实博主在之前已经对design包的各个控件都做了博文说明,无奈个人觉得理解不够深入,所以有了这篇更加深入的介绍,希望各位看官拍砖~ 二.从是什么开始 1.首先我们得知道Coordina ...

  8. 让全链路压测变得更简单!Takin2.0重磅来袭!

    自Takin社区版1.0发布两个多月以来,有很多测试同学陆续在各自的工作中运用了起来,其中包括金融.电商.物流.出行服务等行业.这个过程中我们收到了很多同学的反馈建议,同时也了解到很多同学在落地全链路 ...

  9. Android Material Design简单使用

    吐槽 作为一个 Android developer,没有什么比拿着 UI 设计的一堆 iOS 风格的设计 来做需求更恶心的了,基本所有空间都要照着 iOS 来画一遍,Material Design 辣 ...

随机推荐

  1. TCP 的那些事儿-1

    TCP是一个巨复杂的协议,因为他要解决很多问题,而这些问题又带出了很多子问题和阴暗面.所以学习TCP本身是个比较痛苦的过程,但对于学习的过程却能让人有很多收获.关于TCP这个协议的细节,我还是推荐你去 ...

  2. nyoj 214 单调递增子序列(二)

    单调递增子序列(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 ,a2...,an}(0<n<=100000),找出单调递增最长子序列,并求出其长度. ...

  3. 【BZOJ 3308】 3308: 九月的咖啡店 (费用流|二分图最大权匹配)

    3308: 九月的咖啡店 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 244  Solved: 86 Description 深绘里在九份开了一家咖 ...

  4. 【BZOJ 4148】 4148: [AMPPZ2014]Pillars (乱搞)

    4148: [AMPPZ2014]Pillars Time Limit: 5 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 100  Solve ...

  5. [CodeForces-513E2]Subarray Cuts

    题目大意: 给你一个数列,从中选出k个互不重叠的非空子串,定义s[i]为第i个子串的和,求|s[1]-s[2]|+|s[2]-s[3]|+...+|s[k-1]-s[k]|的最大值. 思路: 考虑将绝 ...

  6. Loj10170骑士

    试题描述 在 n×n(1≤n≤10)的棋盘上放 k(0≤k≤n)个国王(可攻击相邻的8个格子),求使它们无法互相攻击的方案总数. 输入 输入有多组方案,每组数据只有一行为两个整数n和k. 输出 每组数 ...

  7. iOS开发系列--音频播放、录音、

    音频 在iOS中音频播放从形式上可以分为音效播放和音乐播放.前者主要指的是一些短音频播放,通常作为点缀音频,对于这类音频不需要进行进度.循环等控制.后者指的是一些较长的音频,通常是主音频,对于这些音频 ...

  8. php curl 抓取

    <?php set_time_limit(0); function curl_multi($urls) { if (!is_array($urls) or count($urls) == 0) ...

  9. C++输出上三角/下三角/菱形/杨辉三角形

    1.输出上三角形 第一行1个星,第二行3个星,第三行5个星,第四行7个星,第五行9个星. 分析:三角形的形状由输出的空白和星组成,通过分析每一行输出几个空格,几个星,就可完成输出三角形的工作. #in ...

  10. oracle维护服务 oracle解决方案 oracle售后服务

        为客户提供的oracle 金牌技术服务内容为: 1.电话服务 (7*24)   热线支持电话800-810-0081   每周7天,每天24小时北京技术支持中心每天都有专人值守.以保证及时与客 ...