和尚我最近在调整页面状态栏的效果,主要包括沉浸式状态栏和伪沉浸状态栏(同事唠嗑给定义的玩的)。
前段时间整理过一篇 Android 沉浸式状态栏的多种样式,如今和尚我在稍微的补充一下,都是在平常应用中测试整理的。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网络
图2app
Tips1: Window 是一个很值得研究的类,设置 statusBarColor 属性便可修改状态栏颜色。
Tips2: 若配合打开代码中注释的两行,总体的效果是隐藏掉状态栏高度,标题栏上移,如图2所示,在其余相应的场景下颇有用。ide
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)
}
和尚我是在 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方式 判断处理。ui
下面是和尚个人公众号,欢迎闲来吐槽~spa
本文分享自微信公众号 - 阿策小和尚(gh_8297e718c166)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。.net