您现在的位置是:首页 > 文章详情

Android 沉浸式状态栏 以及 伪沉浸式状态栏

日期:2018-05-20点击:354

      小菜最近在调整页面状态栏的效果,主要包括沉浸式状态栏伪沉浸状态栏(同事唠嗑给定义的玩的)。
      前段时间整理过一篇 Android 沉浸式状态栏的多种样式,现在小菜在稍微的补充一下,都是在日常应用中测试整理的。


非 Toolbar 标题栏

      就小菜接触的项目中根据业务不同,不是所有的标题栏都是 Toolbar 标题栏样式,很多是自定义的标题栏样式,为了效果统一,小菜的解决方案是修改顶部状态栏的颜色为程序的主题色,戏称为伪沉浸式状态栏。
      以下是小菜自己测试的最简单的标题栏样式:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <FrameLayout android:layout_width="match_parent" android:layout_height="46dp" android:background="@color/colorAccent" android:gravity="center"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left|center" android:paddingLeft="6dp" android:src="@mipmap/icon_back_white" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="我是标题" android:textColor="@android:color/white" android:textSize="18sp" /> </FrameLayout> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="我是内容" android:textSize="18sp" /> </LinearLayout> 
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_toolbar) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { val window = window window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) window.statusBarColor = resources.getColor(R.color.colorAccent) //window.statusBarColor = Color.TRANSPARENT //window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN } } 
图1.jpg
图2.jpg

Tips1: Window 是一个很值得研究的类,设置 statusBarColor 属性即可修改状态栏颜色。
Tips2: 若配合打开代码中注释的两行,整体的效果是隐藏掉状态栏高度,标题栏上移,如图2所示,在其他相应的场景下很有用。


Toolbar 标题栏

      小菜花了不少时间在以前的博客中,大家可以移步审查一下。现在小菜又用了一种方式,主要是为了满足实时网络更换主题图,采用 背景主题色+透明图层 方式。如果不需要来回更换图片可以直接用 layer-list 的 drawable 方式,现在需要随意更换图片,所以小菜把这主题色和透明涂层区分开。
      以下是小菜加载一张图片的 Toolbar 方式:

<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.roating.ace.ace06.ToolbarTestActivityK"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorAccent" android:gravity="center" android:orientation="vertical"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/icon_bg" android:fitsSystemWindows="true" android:gravity="center" /> </LinearLayout> </android.support.constraint.ConstraintLayout> 
override fun onCreate(savedInstanceState: Bundle?) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { val window = window window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) window.statusBarColor = Color.TRANSPARENT window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN } super.onCreate(savedInstanceState) setContentView(R.layout.activity_toolbar_test) } 
图3.jpg

      小菜是在 Toolbar 外添加一层 LinearLayout 作为背景主题色涂层,Toolbar 添加背景图,而 Toolbar 的位置宽高等均可按需求定义,并配合上面刚提到的 Tips2 方式处理以下。


Tips1: 网上有人说 window 的设置需要放在 setContentView 加载布局文件之前,小菜特意用前后两种位置尝试,效果是一致的。
Tips2: 在使用 window.statusBarColor 时,会提示:Call requires API level 21(current min is 15):android.view.Window#setStatusBarColor,此时不建议用 @TargetApi(Build.VERSION_CODES.KITKAT) 这种方式,这样会固定一个版本,且顶部状态栏有时会修改无效,建议用如上 if方式 判断处理。


      下面是小菜的公众号,欢迎闲来吐槽~


小菜公众号
原文链接:https://yq.aliyun.com/articles/633546
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章