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
}
}
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)
}
小菜是在 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方式 判断处理。
下面是小菜的公众号,欢迎闲来吐槽~
关注公众号
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
安卓——之ListView和SQLite数据库写一个联系人
安卓的数据存储分为五大类:SharedPreferences存储、文件存储(内部存储、外部SD卡存储)、SQLite数据显卡存储、 ContentProvider 、网络存储。 SharedPreferences:它是Android提供的用来存储一些简单的配置信息的一种机制,采用XML格式将数据存储到设备中,常应用于存储程序的各种配置信息,如用户名、密码。可应用于登陆的记住密码。 SQLite数据库:是Android自带的一个轻量级数据库。 文件存储:主要就是Java的IO流操作,不过要注意权限的设置。 最近写了一个安卓作业,要实现添加联系人删除联系人查找联系人并进行SQLite数据库操作。 中途踩到了些许坑: 1.数据库中id是自动增长的,而且id一旦创建是不会改变的,这就导致在删除操作时出现点击获取的动态id无法匹配数据库中静态的id以至于从数据库删除数据失败。 2.本人喜欢自定义设计界面分布及色调,但在自定义对话框时,由于对话框最后调用了dismiss()方法销毁视图,导致获取的数据传过去主界面就变成了null数据。 更新坑点:3.这个坑点是等我写完文章的第二天才测试到的。。。就...
-
下一篇
Android无法打开相册查看视频
最近公司做了一个项目需要查看手机视频,在android 8的模拟器上正常。在android 5.1的模拟器下却报了一个错误: Caused by: java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.PICK dat=content://media/external/video/media cmp=com.android.music/.VideoBrowserActivity } from ProcessRecord{1b308dad 5422:com.videoclipper.demo/u0a58} (pid=5422, uid=10058) not exported from uid 10036 讲道理不应该有权限问题的。因为target为21,而且api22没有运行时权限,android 8也可以正常运行。这个现象真的很莫名其妙啊。经过搜索找到了一种解决方法:原文 // contentId will have the video content id...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Crontab安装和使用
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- Red5直播服务器,属于Java语言的直播服务器
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- MySQL数据库中FOR UPDATE的使用
- CentOS6,CentOS7官方镜像安装Oracle11G

微信收款码
支付宝收款码