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

Android 沉浸式状态栏的多种样式

日期:2018-04-23点击:364

      小菜最近正在处理客户端顶部沉浸式展示图片,借此整理了一下小菜自己研究测试的沉浸式状态栏。
      沉浸式状态栏大家都很熟悉,即 APP 界面图片延伸到状态栏, 应用本身沉浸于状态栏,即顶部不会默认展示系统的黑条。因为小菜技术有限,理解不透彻,所以仅分享一下自己应用测试中可以呈现的几种样式。


基本样式

公共的步骤:

  1. 布局文件中添加使用 Toolbar 控件(纯色 Toolbar 背景色为颜色,图片 Toolbar 样式设置背景色为图片或添加一个 ImageView 控件),在文件根布局与 Toolbar 中添加 android:fitsSystemWindows="true",这个很重要,可以使背景图片延伸至状态栏,当然在 Java 文件中设置一样的效果;
<?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:background="@android:color/white" android:fitsSystemWindows="true" android:orientation="vertical"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorAccent" android:fitsSystemWindows="true"> </android.support.v7.widget.Toolbar> </LinearLayout> 
  1. 设置主题样式,一般是三个 style.xml 文件,分别是 values (默认)、 values-v19 (处理 Android4.4 版本) 和 values-v21 (处理 Android5.0以后的半透明);
values style.xml <style name="ToorbarTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:windowNoTitle">true</item> <item name="android:windowFullscreen">false</item> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowActionBar">false</item> </style> 
values-v19 style.xml <style name="ToorbarTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:windowNoTitle">true</item> <item name="android:windowFullscreen">false</item> <item name="android:windowTranslucentStatus">false</item> <item name="android:windowTranslucentNavigation">true</item> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> <item name="android:windowIsTranslucent">true</item> </style> 
values-v21 style.xml <style name="ToorbarTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:windowNoTitle">true</item> <item name="android:windowFullscreen">false</item> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowActionBar">false</item> </style> 
  1. Java 代码中处理导航栏变黑和透明的主题版本判断;
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.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN } 

纯色 Toolbar 样式

正常纯色 Toolbar 样式

      纯色 Toolbar 在使用中一般会将顶部状态栏设置与 Toolbar 背景色一致;

val window = window window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) window.statusBarColor = resources.getColor(R.color.colorAccent) 

被遮挡操作栏 Toolbar

      在测试过程中会出现底部虚拟操作按纽栏目被遮挡,如下图,此时应注意设置 systemUiVisibility 属性。

window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 

无状态栏 Toolbar 样式(一般不会应用)

无状态栏 Toolbar 样式

无状态栏 Toolbar 样式一般不会在日常中使用,但是测试的过程中发现,分享给大家,其根本原因是主题中 <item name="android:windowFullscreen">true</item>,其他处理上面完全相同。

<style name="ToorbarNoBarTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:windowBackground">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowFullscreen">true</item> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style> 

图片 Toolbar 样式

图片 Toolbar 样式

      图片 Toolbar 样式,一般分两种:一种是设置 Toolbar 背景图 background;另一种是添加一个 ImageView 控件。小菜用的是作为 Toolbar 背景图 background 方式处理,使用 ImageView 控件时还需要单独处理图片,并有部分拉伸的可能。

图片作为布局背景沉浸样式

图片布局背景

图片背景被拉伸
  1. 图片作为布局背景的方式比较简单,方式与公共的相同,只是不需要 Toolbar 而已。
  2. 测试发现,若将根布局的高设为 android:layout_height="wrap_content" 时图片正常展示,如果为 android:layout_height="match_parent" 则图片会被拉伸,当然小菜认为根布局设置为 wrap_content 方式是不合理的。
  3. 小菜的解决方法是使用 layer-list 的 drawable,类似于启动页初始加载时的样式。
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/colorPrimary"></item> <item android:top="26dp"> <bitmap android:gravity="top|center_horizontal" android:src="@mipmap/icon_bg" /> </item> </layer-list> 

      Tips1:还有一种样式与沉浸式展示效果一样,就是折叠布局 CollapsingToolbarLayout 折叠后的效果也是沉浸式状态,可以固定折叠后的状态,但是并不建议这样处理,只是偶然想到而已,各位有兴趣可以研究一下。
      Tips2:使用 Toolbar 时,建议不要再多添加一层布局 Layout,需要的话可以用 CoordinatorLayout。

Screenshot_20180424-224557.jpg

<!-- 不建议用: --> <LinearLayout android:id="@+id/user_header_new_login_lay" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical"> <android.support.v7.widget.Toolbar android:layout_width="match_parent" android:id="@+id/test_tb" android:fitsSystemWindows="true" android:layout_height="100dp"> </android.support.v7.widget.Toolbar> </LinearLayout> <!-- 建议使用: --> <android.support.design.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:fitsSystemWindows="true" app:elevation="0dp"> <android.support.v7.widget.Toolbar android:layout_width="match_parent" android:id="@+id/test_tb" android:fitsSystemWindows="true" android:layout_height="100dp"> </android.support.v7.widget.Toolbar> </android.support.design.widget.AppBarLayout> </android.support.design.widget.CoordinatorLayout> 

      GitHub 实例


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


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

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

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

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

文章评论

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

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章