更简单更全的material design状态栏
从实际使用须要出发,以最简单的方式实现了几种类型的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:
- <android.support.design.widget.CoordinatorLayout>
- <android.support.design.widget.AppBarLayout>
- <android.support.v7.widget.Toolbar
- android:layout_height="wrap_content"
- android:fitsSystemWindows="true"/>
- </android.support.design.widget.AppBarLayout>
- </android.support.design.widget.CoordinatorLayout>
.java:
- /**
- * 简单型状态栏(ToolBar)
- *
- * @param activity
- */
- public static void setOrdinaryToolBar(Activity activity) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
- activity.getWindow().setStatusBarColor(ContextCompat.getColor(activity, R.color.colorPrimaryDark));
- } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
- setKKStatusBar(activity, R.color.colorPrimaryDark);
- }
- }
效果:
左:Android4.4 右:Android6.0
2,图片全屏透明状态栏(图片位于状态栏以下)
layout:
- <android.support.design.widget.CoordinatorLayout>
- <ImageView/>
- <android.support.v7.widget.Toolbar
- android:layout_height="wrap_content"
- android:fitsSystemWindows="true"/>
- </android.support.design.widget.CoordinatorLayout>
.java
/** * 图片全屏透明状态栏(图片位于状态栏以下) * * @param activity */ public static void setImageTransparent(Activity activity) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); }}
效果:
左:Android4.4 右:Android6.0
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
3,图片全屏半透明状态栏(图片位于状态栏以下)
layout:
- <android.support.design.widget.CoordinatorLayout>
- <ImageView/>
- <android.support.v7.widget.Toolbar
- android:layout_height="wrap_content"
- android:fitsSystemWindows="true"/>
- </android.support.design.widget.CoordinatorLayout>
.java:
- /**
- * 图片全屏半透明状态栏(图片位于状态栏以下)
- *
- * @param activity
- */
- public static void setImageTranslucent(Activity activity) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
- activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
- activity.getWindow().setStatusBarColor(ContextCompat.getColor(activity, R.color.statusBar));
- } else {
- setKKStatusBar(activity, R.color.statusBar);
- }
- }
效果:
左:Android4.4 右:Android6.0
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
4,ToolBar+TabLayout状态栏(ToolBar可伸缩)
layout:
- <android.support.design.widget.CoordinatorLayout
- android:background="@color/colorPrimaryDark"
- android:fitsSystemWindows="true">
- <android.support.design.widget.AppBarLayout>
- <android.support.v7.widget.Toolbar
- android:layout_height="?attr/actionBarSize"
- android:background="@color/colorPrimary"
- app:layout_scrollFlags="scroll|enterAlways|snap"/>
- <android.support.design.widget.TabLayout/>
- </android.support.design.widget.AppBarLayout>
- <android.support.v4.widget.NestedScrollView/>
- </android.support.design.widget.CoordinatorLayout>
.java:
- /**
- * ToolBar+TabLayout状态栏(ToolBar可伸缩)
- *
- * @param activity
- */
- public static void setToolbarTabLayout(Activity activity) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
- activity.getWindow().setStatusBarColor(ContextCompat.getColor(activity, R.color.colorPrimaryDark));
- }
- }
效果:
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:
- <android.support.v4.widget.DrawerLayout
- android:fitsSystemWindows="true">
- <android.support.design.widget.CoordinatorLayout
- android:background="@color/colorPrimary">
- <android.support.design.widget.AppBarLayout>
- <android.support.v7.widget.Toolbar
- android:layout_height="wrap_content"
- app:layout_scrollFlags="scroll|enterAlways|snap"/>
- <android.support.design.widget.TabLayout/>
- </android.support.design.widget.AppBarLayout>
- <android.support.v4.widget.NestedScrollView
- android:background="@android:color/white"
- app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
- </android.support.design.widget.CoordinatorLayout>
- <android.support.design.widget.NavigationView
- android:fitsSystemWindows="true"/>
- </android.support.v4.widget.DrawerLayout>
.java:
- /**
- * DrawerLayout+ToolBar+TabLayout状态栏(ToolBar可伸缩)
- *
- * @param activity
- * @param drawerLayout
- * @param coordinatorLayout
- */
- public static void setDrawerToolbarTabLayout(Activity activity, CoordinatorLayout coordinatorLayout) {
- if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
- ViewGroup contentLayout = (ViewGroup) activity.findViewById(android.R.id.content);
- contentLayout.getChildAt(0).setFitsSystemWindows(false);
- coordinatorLayout.setFitsSystemWindows(true);
- setKKStatusBar(activity, R.color.statusBar);
- }
- }
效果:
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:
- <android.support.design.widget.CoordinatorLayout
- android:fitsSystemWindows="true">
- <android.support.design.widget.AppBarLayout
- android:fitsSystemWindows="true">
- <android.support.design.widget.CollapsingToolbarLayout>
- <ImageView
- android:fitsSystemWindows="true"/>
- <android.support.v7.widget.Toolbar
- android:layout_height="?attr/actionBarSize"
- app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"/>
- </android.support.design.widget.CollapsingToolbarLayout>
- </android.support.design.widget.AppBarLayout>
- <android.support.v4.widget.NestedScrollView
- app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
- </android.support.design.widget.CoordinatorLayout>
.java:
- /**
- * CollapsingToolbarLayout状态栏(可折叠图片)
- *
- * @param activity
- * @param coordinatorLayout
- * @param appBarLayout
- * @param imageView
- * @param toolbar
- */
- public static void setCollapsingToolbar(Activity activity, CoordinatorLayout coordinatorLayout,
- AppBarLayout appBarLayout, ImageView imageView, Toolbar toolbar) {
- if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
- coordinatorLayout.setFitsSystemWindows(false);
- appBarLayout.setFitsSystemWindows(false);
- imageView.setFitsSystemWindows(false);
- toolbar.setFitsSystemWindows(true);
- CollapsingToolbarLayout.LayoutParams lp = (CollapsingToolbarLayout.LayoutParams) toolbar.getLayoutParams();
- lp.height = (int) (getStatusBarHeight(activity) +
- activity.getResources().getDimension(R.dimen.abc_action_bar_default_height_material));
- toolbar.setLayoutParams(lp);
- setKKStatusBar(activity, R.color.statusBar);
- setCollapsingToolbarStatus(appBarLayout);
- }
- }
效果:
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:
- <android.support.design.widget.CoordinatorLayout>
- <android.support.design.widget.AppBarLayout>
- <android.support.v7.widget.Toolbar
- android:layout_height="wrap_content"
- android:fitsSystemWindows="true"/>
- </android.support.design.widget.AppBarLayout>
- <include layout="@layout/content_main"/>
- </android.support.design.widget.CoordinatorLayout>
.java:
- /**
- * DrawerLayout+ToolBar型状态栏
- *
- * @param activity
- */
- public static void setDrawerToolbar(Activity activity) {
- if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
- ViewGroup contentLayout = (ViewGroup) activity.findViewById(android.R.id.content);
- contentLayout.getChildAt(0).setFitsSystemWindows(false);
- setKKStatusBar(activity, R.color.statusBar);
- }
- }
其他:
- /**
- * Android4.4上CollapsingToolbar折叠时statusBar显示和隐藏
- *
- * @param appBarLayout
- */
- private static void setCollapsingToolbarStatus(AppBarLayout appBarLayout) {
- ViewCompat.setAlpha(mStatusBarView, 1);
- appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
- @Override
- public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
- int maxScroll = appBarLayout.getTotalScrollRange();
- float percentage = (float) Math.abs(verticalOffset) / (float) maxScroll;
- ViewCompat.setAlpha(mStatusBarView, percentage);
- }
- });
- }
- private static void setKKStatusBar(Activity activity, int statusBarColor) {
- ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
- mStatusBarView = contentView.getChildAt(0);
- //改变颜色时避免反复加入statusBarView
- if (mStatusBarView != null && mStatusBarView.getMeasuredHeight() == getStatusBarHeight(activity)) {
- mStatusBarView.setBackgroundColor(ContextCompat.getColor(activity, statusBarColor));
- return;
- }
- mStatusBarView = new View(activity);
- ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
- getStatusBarHeight(activity));
- mStatusBarView.setBackgroundColor(ContextCompat.getColor(activity, statusBarColor));
- contentView.addView(mStatusBarView, lp);
- }
- private static int getStatusBarHeight(Context context) {
- int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
- return context.getResources().getDimensionPixelSize(resourceId);
- }
效果:
Android4.4:
Android6.0:
OK!
更简单更全的material design状态栏的更多相关文章
- 采用ADM2483磁隔离器让RS485接口更简单更安全
采用ADM2483磁隔离器让RS485接口更简单更安全 摘要:本文介绍RS485的特点及应用,指出了普通RS485接口易损坏的问题,针对存在的问题介绍了以ADM2483为核心的磁隔离解决方案. 关键词 ...
- TSQL:让监控分析更简单更高效
1. 前言 阿里时序时空数据库TSDB最新推出TSQL,支持标准SQL的语法和函数.用户使用熟悉的SQL,不仅仅查询更简单易用,用户还可以利用SQL强大的功能,实现更加复杂的计算分析. 2. 为什么需 ...
- Anno 让微服务、混合编程更简单(Net love Java)
在社区或者QQ群我们经常看到有人争辩编程语言的好坏,只要一提起这个话题常常就能引来很多人参与,往往最后就变成了一群人几个小时的骂战.今天我们要说的是如何让Java和.Net(甚至更多语言)相结合.充分 ...
- [翻译]Kafka Streams简介: 让流处理变得更简单
Introducing Kafka Streams: Stream Processing Made Simple 这是Jay Kreps在三月写的一篇文章,用来介绍Kafka Streams.当时Ka ...
- Kafka Streams简介: 让流处理变得更简单
Introducing Kafka Streams: Stream Processing Made Simple 这是Jay Kreps在三月写的一篇文章,用来介绍Kafka Streams.当时Ka ...
- 走着官方的教程入门Material Design(一)
又到期末了,学习下Google的材料设计.写下此文记录自己的同时,分享给需要的同学,若发现文中有什么问题和不对,欢迎指出 使用 Material Design 创建新应用 首先需要使用材料主题 如果是 ...
- 【知识必备】一文让你搞懂design设计的CoordinatorLayout和AppbarLayout联动,让Design设计更简单~
一.写在前面 其实博主在之前已经对design包的各个控件都做了博文说明,无奈个人觉得理解不够深入,所以有了这篇更加深入的介绍,希望各位看官拍砖~ 二.从是什么开始 1.首先我们得知道Coordina ...
- 让全链路压测变得更简单!Takin2.0重磅来袭!
自Takin社区版1.0发布两个多月以来,有很多测试同学陆续在各自的工作中运用了起来,其中包括金融.电商.物流.出行服务等行业.这个过程中我们收到了很多同学的反馈建议,同时也了解到很多同学在落地全链路 ...
- Android Material Design简单使用
吐槽 作为一个 Android developer,没有什么比拿着 UI 设计的一堆 iOS 风格的设计 来做需求更恶心的了,基本所有空间都要照着 iOS 来画一遍,Material Design 辣 ...
随机推荐
- TCP 的那些事儿-1
TCP是一个巨复杂的协议,因为他要解决很多问题,而这些问题又带出了很多子问题和阴暗面.所以学习TCP本身是个比较痛苦的过程,但对于学习的过程却能让人有很多收获.关于TCP这个协议的细节,我还是推荐你去 ...
- nyoj 214 单调递增子序列(二)
单调递增子序列(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 ,a2...,an}(0<n<=100000),找出单调递增最长子序列,并求出其长度. ...
- 【BZOJ 3308】 3308: 九月的咖啡店 (费用流|二分图最大权匹配)
3308: 九月的咖啡店 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 244 Solved: 86 Description 深绘里在九份开了一家咖 ...
- 【BZOJ 4148】 4148: [AMPPZ2014]Pillars (乱搞)
4148: [AMPPZ2014]Pillars Time Limit: 5 Sec Memory Limit: 256 MBSec Special JudgeSubmit: 100 Solve ...
- [CodeForces-513E2]Subarray Cuts
题目大意: 给你一个数列,从中选出k个互不重叠的非空子串,定义s[i]为第i个子串的和,求|s[1]-s[2]|+|s[2]-s[3]|+...+|s[k-1]-s[k]|的最大值. 思路: 考虑将绝 ...
- Loj10170骑士
试题描述 在 n×n(1≤n≤10)的棋盘上放 k(0≤k≤n)个国王(可攻击相邻的8个格子),求使它们无法互相攻击的方案总数. 输入 输入有多组方案,每组数据只有一行为两个整数n和k. 输出 每组数 ...
- iOS开发系列--音频播放、录音、
音频 在iOS中音频播放从形式上可以分为音效播放和音乐播放.前者主要指的是一些短音频播放,通常作为点缀音频,对于这类音频不需要进行进度.循环等控制.后者指的是一些较长的音频,通常是主音频,对于这些音频 ...
- php curl 抓取
<?php set_time_limit(0); function curl_multi($urls) { if (!is_array($urls) or count($urls) == 0) ...
- C++输出上三角/下三角/菱形/杨辉三角形
1.输出上三角形 第一行1个星,第二行3个星,第三行5个星,第四行7个星,第五行9个星. 分析:三角形的形状由输出的空白和星组成,通过分析每一行输出几个空格,几个星,就可完成输出三角形的工作. #in ...
- oracle维护服务 oracle解决方案 oracle售后服务
为客户提供的oracle 金牌技术服务内容为: 1.电话服务 (7*24) 热线支持电话800-810-0081 每周7天,每天24小时北京技术支持中心每天都有专人值守.以保证及时与客 ...