这里我介绍的是我比较常用的一种方式,也是网上很多都有介绍的,不多说直接上代码
/**
* 这个方法是控制布局控件是否顶在状态栏上(fitSystemWindows=true,避免布局控件顶在状态栏上)(itSystemWindows=true,布局控件顶在状态栏上)
*/
public static void setRootViewFitsSystemWindows(Activity activity, boolean fitSystemWindows) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
ViewGroup winContent = (ViewGroup) activity.findViewById(android.R.id.content);
if (winContent.getChildCount() > 0) {
ViewGroup rootView = (ViewGroup) winContent.getChildAt(0);
if (rootView != null) {
rootView.setFitsSystemWindows(fitSystemWindows);
}
}
}
}
确保页面布局把状态栏的位置占据以后,再设置状态栏的背景色为透明就ok了,设置状态栏透明的方法如下:
/**
* 设置状态栏为透明
* @param activity
*/
@TargetApi(19)
public static void setLucencyStatus(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = activity.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = activity.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
到这里就简单的实现状态栏的沉浸和透明了,因为一般开发过程中,经常会遇到需要设置沉浸效果。但是只有Android版本4.4以后才支持状态栏沉浸。
首先涉及到不同品牌手机的生态系统,所以需要做不同的适配,这里判断的工具类也是我从网上找的,我看好多人都用了,我这里也贴一下,如有侵权,请联系我,我立马删除!!!
import android.os.Environment;
import android.text.TextUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class OSUtil {
//MIUI标识
private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";
private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage";
//EMUI标识
private static final String KEY_EMUI_VERSION_CODE = "ro.build.version.emui";
private static final String KEY_EMUI_API_LEVEL = "ro.build.hw_emui_api_level";
private static final String KEY_EMUI_CONFIG_HW_SYS_VERSION = "ro.confg.hw_systemversion";
//Flyme标识
private static final String KEY_FLYME_ID_FALG_KEY = "ro.build.display.id";
private static final String KEY_FLYME_ID_FALG_VALUE_KEYWORD = "Flyme";
private static final String KEY_FLYME_ICON_FALG = "persist.sys.use.flyme.icon";
private static final String KEY_FLYME_SETUP_FALG = "ro.meizu.setupwizard.flyme";
private static final String KEY_FLYME_PUBLISH_FALG = "ro.flyme.published";
/**
* 是否是Flyme系统
* @return
*/
public static boolean isFlyme() {
if(isPropertiesExist(KEY_FLYME_ICON_FALG, KEY_FLYME_SETUP_FALG, KEY_FLYME_PUBLISH_FALG)) {
return true;
}
try {
BuildProperties buildProperties = BuildProperties.newInstance();
if(buildProperties.containsKey(KEY_FLYME_ID_FALG_KEY)) {
String romName = buildProperties.getProperty(KEY_FLYME_ID_FALG_KEY);
if (!TextUtils.isEmpty(romName) && romName.contains(KEY_FLYME_ID_FALG_VALUE_KEYWORD)) {
return true;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
* 是否是EMUI系统
* @return
*/
public static boolean isEMUI() {
return isPropertiesExist(KEY_EMUI_VERSION_CODE, KEY_EMUI_API_LEVEL,
KEY_EMUI_CONFIG_HW_SYS_VERSION);
}
/**
* 是否是MIUI系统
* @return
*/
public static boolean isMIUI() {
return isPropertiesExist(KEY_MIUI_VERSION_CODE, KEY_MIUI_VERSION_NAME,
KEY_MIUI_INTERNAL_STORAGE);
}
private static boolean isPropertiesExist(String... keys) {
if (keys == null || keys.length == 0) {
return false;
}
try {
BuildProperties properties = BuildProperties.newInstance();
for (String key : keys) {
String value = properties.getProperty(key);
if (value != null)
return true;
}
return false;
} catch (IOException e) {
return false;
}
}
private static final class BuildProperties {
private final Properties properties;
private BuildProperties() throws IOException {
properties = new Properties();
// 读取系统配置信息build.prop类
properties.load(new FileInputStream(new File(Environment.getRootDirectory(), "build" +
".prop")));
}
public boolean containsKey(final Object key) {
return properties.containsKey(key);
}
public boolean containsValue(final Object value) {
return properties.containsValue(value);
}
public Set> entrySet() {
return properties.entrySet();
}
public String getProperty(final String name) {
return properties.getProperty(name);
}
public String getProperty(final String name, final String defaultValue) {
return properties.getProperty(name, defaultValue);
}
public boolean isEmpty() {
return properties.isEmpty();
}
public Enumeration
通过下面方法修改状态栏的背景颜色和字体颜色:
/**
* 设置状态栏模式
*
* @param activity
* @param isTextDark 文字、图标是否为深色 (false为默认的白色)
* @param colorId 状态栏颜色
* @return
*/
public static void setStatusBarMode(Activity activity, boolean isTextDark, int colorId) {
if (!isTextDark) {
//文字、图标颜色不变,只修改状态栏颜色
setStatusBarColor(activity, colorId);
} else {
//修改状态栏颜色和文字图标颜色
setStatusBarColor(activity, colorId);
//4.4以上才可以改文字图标颜色
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (OSUtil.isMIUI()) {
//小米MIUI系统
setMIUIStatusBarTextMode(activity, isTextDark);
} else if (OSUtil.isFlyme()) {
//魅族flyme系统
setFlymeStatusBarTextMode(activity, isTextDark);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//6.0以上,调用系统方法
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
} else {
//4.4以上6.0以下的其他系统,暂时没有修改状态栏的文字图标颜色的方法,有可以加上
}
}
}
}
/**
* 设置Flyme系统状态栏的文字图标颜色
*
* @param activity
* @param isDark 状态栏文字及图标是否为深色
* @return
*/
public static boolean setFlymeStatusBarTextMode(Activity activity, boolean isDark) {
Window window = activity.getWindow();
boolean result = false;
if (window != null) {
try {
WindowManager.LayoutParams lp = window.getAttributes();
Field darkFlag = WindowManager.LayoutParams.class
.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
Field meizuFlags = WindowManager.LayoutParams.class
.getDeclaredField("meizuFlags");
darkFlag.setAccessible(true);
meizuFlags.setAccessible(true);
int bit = darkFlag.getInt(null);
int value = meizuFlags.getInt(lp);
if (isDark) {
value |= bit;
} else {
value &= ~bit;
}
meizuFlags.setInt(lp, value);
window.setAttributes(lp);
result = true;
} catch (Exception e) {
}
}
return result;
}
/**
* 设置MIUI系统状态栏的文字图标颜色(MIUIV6以上)
*
* @param activity
* @param isDark 状态栏文字及图标是否为深色
* @return
*/
public static boolean setMIUIStatusBarTextMode(Activity activity, boolean isDark) {
boolean result = false;
Window window = activity.getWindow();
if (window != null) {
Class clazz = window.getClass();
try {
int darkModeFlag = 0;
Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
darkModeFlag = field.getInt(layoutParams);
Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
if (isDark) {
extraFlagField.invoke(window, darkModeFlag, darkModeFlag);//状态栏透明且黑色字体
} else {
extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字体
}
result = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//开发版 7.7.13 及以后版本采用了系统API,旧方法无效但不会报错,所以两个方式都要加上
if (isDark) {
activity.getWindow().getDecorView().setSystemUiVisibility(View
.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View
.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
} else {
activity.getWindow().getDecorView().setSystemUiVisibility(View
.SYSTEM_UI_FLAG_VISIBLE);
}
}
} catch (Exception e) {
}
}
return result;
}
/**
* 修改状态栏颜色,支持4.4以上版本
*
* @param activity
* @param colorId
*/
public static void setStatusBarColor(Activity activity, int colorId) {
//Android6.0(API 23)以上,系统方法
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = activity.getWindow();
window.setStatusBarColor(activity.getResources().getColor(colorId));
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//使用SystemBarTint库使4.4版本状态栏变色,需要先将状态栏设置为透明
setTranslucentStatus(activity);
//设置状态栏颜色
SystemBarTintManager tintManager = new SystemBarTintManager(activity);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(colorId);
}
}
以上工具类均来自网上,如有侵权请联系我删除!!!
下面才是我开这个博客的重点 ,状态栏沉浸设置失效!!如果不是Android4.4以下版本的手机,请看我下面的讲解!!
因为状态栏的沉浸一般们还会在布局里面设置
android:fitsSystemWindows=“true”
如上面的代码中设置了android:fitsSystemWindows,或者你在style文件里面是设置了:
- true
那么你在Activity里面通过上面的方法设置布局控件顶在状态栏上就可能会失效,我遇到过有的页面行,比如在fragment里面能设置:
setRootViewFitsSystemWindows(Activity.this, false);
setTranslucentStatus(Activity.this);
这样设置会出现状态栏沉浸不起作用,你只需要把上面布局里面或者style文件里面把这些删除,就OK了,删除这些以后你可以不需要设置
setRootViewFitsSystemWindows(Activity.this, false);
因为默认就是false,你只需要设置透明状态栏就行了!!!
希望我的博客能帮助到你!!!
附上我最近整理的一个小demo,里面设置了状态栏透明,以及状态栏沉浸,自定义状态栏高度等,需要的同学可以看看,demo里面还有抽屉效果、导航切换、夜间模式等!!!
https://github.com/KuoLuoC/AndroidDemo1