现在越来越多的软件都开始使用沉浸式状态栏了,下面总结一下沉浸式状态栏的两种使用方法

注意!沉浸式状态栏只支持安卓4.4及以上的版本
状态栏:4.4上是渐变色,5.0上是完全透明,本文模拟器为4.4演示
效果图:
注意!两种方法的区别:
第一种:为顶部栏跟随当前activity的布局文件的背景的颜色,使用方便,不过也有点问题就是,如果有底部虚拟导航键的话,导航键的背景跟顶部的颜色一样,比如:
第二种:是通过设置顶部栏的颜色来显示的,可以解决第一种的不足,比如:
第一种使用方法:
第一、首先在values、values-v19、values-v21文件夹下的styles.xml都设置一个 Translucent System Bar 风格的Theme,如下图:
values/style.xml:
<style name="TranslucentTheme" parent="AppTheme"> <!--在Android 4.4之前的版本上运行,直接跟随系统主题--> </style> values-v19/style.xml: <style name="TranslucentTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="android:windowTranslucentStatus">true</item> <item name="android:windowTranslucentNavigation">true</item> </style>
values-v21/style.xml:
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="android:windowTranslucentStatus">false</item> <item name="android:windowTranslucentNavigation">true</item> <!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色--> <item name="android:statusBarColor">@android:color/transparent</item> </style>
第二、在清单文件中配置需要沉浸式状态栏的activity加入theme
<activity android:name=".ImageActivity" android:theme="@style/TranslucentTheme" /> <activity android:name=".ColorActivity" android:theme="@style/TranslucentTheme" />
第三、在Activity的布局文件中的跟布局加入“android:fitsSystemWindows=”true””,但是,这里需要区分一下,就是背景是图片还是纯色:
1.当背景为图片时,布局可以这么写:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/imgs_bj" android:fitsSystemWindows="true"> </RelativeLayout>
效果:
2.当背景为纯色,我们需要对布局划分一下,标题布局与内容布局,先把根布局背景设置成标题布局的背景色,然后标题背景色可以不用设置直接使用根布局的背景色,最后内容布局背景色设置为白色
<?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="@color/colorPrimary" //根布局背景设置成“标题布局”想要的颜色
android:fitsSystemWindows="true"
android:orientation="vertical">
<!--标题布局-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="@color/color_31c27c">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="这是标题"
android:textColor="@android:color/white"
android:textSize="20sp" />
</RelativeLayout>
<!--内容布局-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" //内容区域背景设置成白色
android:gravity="center"
android:orientation="vertical">
<Button
android:layout_marginTop="120dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="显示信息"
android:onClick="showMsg"
/>
</LinearLayout>
</LinearLayout>
效果图:
好了,以上就是沉浸式状态栏实现的全过程,但是还有一点值得注意的就是,如果我们activity比较多,每一个页面都添加Android:fitsSystemWindows="true" 比较麻烦,我们需要改动一下:
写一个基类BaseColorActivity.class,代码如下:
public abstract class BaseColorActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//这一行注意!看本文最后的说明!!!!
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(getLayoutResId());//把设置布局文件的操作交给继承的子类
ViewGroup contentFrameLayout = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);
View parentView = contentFrameLayout.getChildAt(0);
if (parentView != null && Build.VERSION.SDK_INT >= 14) {
parentView.setFitsSystemWindows(true);
}
}
/**
* 返回当前Activity布局文件的id
*
* @return
*/
abstract protected int getLayoutResId();
}
然后需要沉浸状态栏的activity继承该基类:
public class ColorActivity extends BaseColorActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//这里不需要写setContentView()!
}
@Override
protected int getLayoutResId() {
//onCreate的方法中不需要写setContentView(),直接把当前activity的布局文件在这里返回就行了!
return R.layout.activity_color;
}
}
然后需要沉浸状态栏的activity的布局文件中就可以把android:fitsSystemWindows="true"这行代码给省略了!
第二种使用方法(未完):
写个工具类StatusBarCompat.class:
public class StatusBarCompat {
private static final int INVALID_VAL = -1;
private static final int COLOR_DEFAULT = Color.parseColor("#20000000");
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void compat(Activity activity, int statusColor)
{
//当前手机版本为5.0及以上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
if (statusColor != INVALID_VAL)
{
activity.getWindow().setStatusBarColor(statusColor);
}
return;
}
//当前手机版本为4.4
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
{
int color = COLOR_DEFAULT;
ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
if (statusColor != INVALID_VAL)
{
color = statusColor;
}
View statusBarView = new View(activity);
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
getStatusBarHeight(activity));
statusBarView.setBackgroundColor(color);
contentView.addView(statusBarView, lp);
}
}
public static void compat(Activity activity)
{
compat(activity, INVALID_VAL);
}
public static int getStatusBarHeight(Context context)
{
int result = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0)
{
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
}
使用方法:
在当前activity的onCreate中,调用方法StatusBarCompat.compat就可以了:
//第二个参数是想要设置的颜色 StatusBarCompat.compat(this, Color.RED);
如果嫌每个activity都要写有点麻烦,那就写个基类来完成这一步:
public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
StatusBarCompat.compat(this, Color.RED);
}
}
然后每个activity的页面继承该BaseActivity就可以了!
关于上面代码中提示注意的那个地方的说明:
隐藏系统title注意的两点:
1、继承AppCompatActivity时使用:
supportRequestWindowFeature(Window.FEATURENOTITLE)
2、继承activity时使用:
requestWindowFeature(Window.FEATURENOTITLE)
文本相关下载:点击免费下载源码及apk文件
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# android
# 沉浸式状态栏
# 沉浸状态栏
# android4.4状态栏沉浸
# Android 实现沉浸式状态栏的方法
# Android 沉浸式状态栏与隐藏导航栏实例详解
# 解决Android 沉浸式状态栏和华为虚拟按键冲突问题
# Android沉浸式状态栏微技巧(带你真正理解沉浸式模式)
# Android 4.4以上"沉浸式"状态栏效果的实现方法
# Android App仿QQ制作Material Design风格沉浸式状态栏
# Android编程中沉浸式状态栏的三种实现方式详解
# Android 高仿QQ 沉浸式状态栏
# 另外两种Android沉浸式状态栏实现思路
# Android实现沉浸式状态栏功能
# 状态栏
# 背景色
# 第一种
# 要写
# 设置成
# 两种
# 不需
# 第二种
# 就可以
# 这是
# 在这里
# 好了
# 子类
# 第二个
# 先把
# 比较多
# 设置为
# 中就
# 来完成
# 大家多多
相关文章:
岳西云建站教程与模板下载_一站式快速建站系统操作指南
网站广告牌制作方法,街上的广告牌,横幅,用PS还是其他软件做的?
如何在万网主机上快速搭建网站?
c++怎么用jemalloc c++替换默认内存分配器【性能】
七夕网站制作视频,七夕大促活动怎么报名?
,如何利用word制作宣传手册?
建站上市公司网站建设方案与SEO优化服务定制指南
香港服务器如何优化才能显著提升网站加载速度?
如何确认建站备案号应放置的具体位置?
Android自定义控件实现温度旋转按钮效果
如何在阿里云虚拟服务器快速搭建网站?
如何快速打造个性化非模板自助建站?
如何用y主机助手快速搭建网站?
如何零成本快速生成个人自助网站?
网站制作专业公司有哪些,如何制作一个企业网站,建设网站的基本步骤有哪些?
linux top下的 minerd 木马清除方法
学校建站服务器如何选型才能满足性能需求?
建站10G流量真的够用吗?如何应对访问高峰?
如何用景安虚拟主机手机版绑定域名建站?
网站制作中优化长尾关键字挖掘的技巧,建一个视频网站需要多少钱?
手机钓鱼网站怎么制作视频,怎样拦截钓鱼网站。怎么办?
整蛊网站制作软件,手机不停的收到各种网站的验证码短信,是手机病毒还是人为恶搞?有这种手机病毒吗?
如何通过西部建站助手安装IIS服务器?
西安市网站制作公司,哪个相亲网站比较好?西安比较好的相亲网站?
香港服务器租用每月最低只需15元?
如何在云虚拟主机上快速搭建个人网站?
寿县云建站:智能SEO优化与多行业模板快速上线指南
如何快速辨别茅台真假?关键步骤解析
如何登录建站主机?访问步骤全解析
广州网站设计制作一条龙,广州巨网网络科技有限公司是干什么的?
如何制作公司的网站链接,公司想做一个网站,一般需要花多少钱?
安徽网站建设与外贸建站服务专业定制方案
Python如何创建带属性的XML节点
企业微网站怎么做,公司网站和公众号有什么区别?
新网站制作渠道有哪些,跪求一个无线渠道比较强的小说网站,我要发表小说?
如何破解联通资金短缺导致的基站建设难题?
香港服务器网站生成指南:免费资源整合与高速稳定配置方案
大连 网站制作,大连天途有线官网?
魔毅自助建站系统:模板定制与SEO优化一键生成指南
制作网站的过程怎么写,用凡科建站如何制作自己的网站?
天津个人网站制作公司,天津网约车驾驶员从业资格证官网?
如何在Windows 2008云服务器安全搭建网站?
网页设计与网站制作内容,怎样注册网站?
XML的“混合内容”是什么 怎么用DTD或XSD定义
jQuery 常见小例汇总
c++如何打印函数堆栈信息_c++ backtrace函数与符号名解析【方法】
微网站制作教程,我微信里的网站怎么才能复制到浏览器里?
建站主机选购指南:核心配置与性价比推荐解析
建站之星备案是否影响网站上线时间?
定制建站模板如何实现SEO优化与智能系统配置?18字教程
*请认真填写需求信息,我们会在24小时内与您取得联系。