关于沉浸状态栏最佳实现实例 博主自己经过了多个demo的经验,遇到许多坑,也踩过许多坑,用过了以前一个开源库systembartint,可自行百度,此处就不讲如何使用了。摸索到现在,现在终于可以总结一下了现在我开始着重讲解安卓版本4.4以后可用两种方法去设置沉浸状态栏。
api版本4.4~5.0 之间的沉浸状态栏是只支持半透明的;博主之前因为用的4.4的模拟器来测试的,也用博主自己的华为手机试过(不是华为手机自身的),一直想解决这个问题,无果; api5.0(lolipop)之后的沉浸浸透状态栏的实现就变简单了,后面会讲解到;
在看代码之前,我们先了解一下谷歌公司给我们提供的各种默认属性的样式
下面这个是窗口的大致视图 1.在代码里面添加实现
//通过设置全屏达到一体化效果,但是看不到状态信息 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏 //设置导航栏透明 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); getWindow().requestFeature(Window.FEATURE_NO_TITLE);//设置为无标题栏2.更详细的实现,其实就是先把原来已经有的flag清除掉,然后重新设置在DecorView上面就能够实现沉浸状态栏了
//API LEVEL>5.0 if(VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { Window window = getWindow(); window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.setStatusBarColor(Color.TRANSPARENT); window.setNavigationBarColor(Color.TRANSPARENT); } //通过设置状态栏透明实现一体化效果,但是会出现渐变效果,4.4 if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT){ //设置沉浸状态栏 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //设置导航栏透明 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); } //设置状态栏的字体模式 设置为黑字 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN| View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); }以上代码可以将他封装成一个类里面的方法随便调用,如下;如果需要动态设置状态栏的颜色可以先通过 getWindow()方法获取window然后再通过setStatusBarColor(Color.RED)去实现,导航栏同样实现
package com.yqq.touchtest; import android.annotation.TargetApi; import android.app.Activity; import android.graphics.Color; import android.os.Build; import android.view.View; import android.view.Window; import android.view.WindowManager; /** * 设置沉浸状态栏的工具类 * @author yqq * */ public class TranslucentStatusSetting { public static void setTranslucentStatusSetting(Activity context){ context.getWindow().requestFeature(Window.FEATURE_NO_TITLE);//设置为无标题栏 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT&&Build.VERSION.SDK_INT<Build.VERSION_CODES.LOLLIPOP) { setTranslucentStatus(true,context); //设置沉浸状态栏 context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //设置导航栏透明 context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); } //API LEVEL>5.0 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Window window = context.getWindow(); window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.setStatusBarColor(Color.TRANSPARENT); window.setNavigationBarColor(Color.TRANSPARENT); } //设置状态栏的字体模式 设置为黑字 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ context.getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN| View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); } } @TargetApi(19) private static void setTranslucentStatus(boolean on,Activity context) { Window win = context.getWindow(); WindowManager.LayoutParams winParams = win.getAttributes(); final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; if (on) { winParams.flags |= bits; } else { winParams.flags &= ~bits; } win.setAttributes(winParams); } } 这里写代码片在res-values-v21文件夹的styles.xml文件中添加相关的属性,因为我这里用的material主题是5.0以后出现的,如果5.0以前需要用的话就需要添加support-v21支持库; 我们在自定义样式的时候大概就是根据上面的图片完成我们基本的需求 为了兼容api19 res-values文件夹下面也要有一个styles.xml文件
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="android:Theme.Material.Light"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="android:statusBarColor">@color/skyblue</item> <item name="android:navigationBarColor">@color/transparent</item> <item name="android:navigationMode">listMode</item> </style> </resources>实例图片 测试效果图
开发中一些相关的小收获希望能帮到大家 官方在Android6.0中提供了亮色状态栏模式(就是改变字体的颜色从白字变成灰黑色字),配置很简单:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { activity.getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN| View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); }或者在style属性中加上
<item name="android:windowLightStatusBar">true</item>values文件夹具有覆盖性质,根据当前安装次应用的手机的系统版本匹配不同的values配置文件夹。例如如果手机是andorid6.0(M),这样的话就会自动匹配values-23,如果,无values-23则找res目录下最大的一个进行匹配。基本的原则是匹配最新版本号
