1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > android 沉浸式状态栏 19 Android 沉浸式状态栏 以及 伪沉浸式状态栏

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

时间:2020-04-07 13:48:40

相关推荐

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

小菜最近在调整页面状态栏的效果,主要包括沉浸式状态栏和伪沉浸状态栏(同事唠嗑给定义的玩的)。

前段时间整理过一篇 Android 沉浸式状态栏的多种样式,现在小菜在稍微的补充一下,都是在日常应用中测试整理的。

非 Toolbar 标题栏

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

以下是小菜自己测试的最简单的标题栏样式:<?xml version="1.0"encoding="utf-8"?>

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="46dp"

android:background="@color/colorAccent"

android:gravity="center">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="left|center"

android:paddingLeft="6dp"

android:class="lazyload" src="https://img-/202612174084872.png" data-original="@mipmap/icon_back_white"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:text="我是标题"

android:textColor="@android:color/white"

android:textSize="18sp"/>

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center"

android:text="我是内容"

android:textSize="18sp"/>

overridefunonCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState)

setContentView(R.layout.activity_toolbar)if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){

valwindow=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_STABLEorView.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"?>

xmlns:tools="/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="com.roating.ace.ace06.ToolbarTestActivityK">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="@color/colorAccent"

android:gravity="center"

android:orientation="vertical">

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"/>

overridefunonCreate(savedInstanceState:Bundle?){if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){

valwindow=window

window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)window.statusBarColor=Color.TRANSPARENTwindow.decorView.systemUiVisibility=View.SYSTEM_UI_FLAG_LAYOUT_STABLEorView.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方式 判断处理。

作者:老菜和尚

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。