android:clipToPadding="true"
android:fitsSystemWindows="true"
添加在父布局XML中
2.改變狀態(tài)欄顏色與背景顏色值??
調(diào)用方法:
package com.example.administrator.myapplicationtest.Tools;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import java.io.File;
import java.io.FileInputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Properties;
/**
* auther Android
* Created by on 2019/4/5.
* Describe :
*/
public class StatusBarUtil {
class AvailableRomType {
public static final int MIUI =1;
? ? ? ? public static final int FLYME =2;
? ? ? ? public static final int ANDROID_NATIVE =3;
? ? ? ? public static final int NA =4;
? ? }
public static int getLightStatusBarAvailableRomType() {
//開發(fā)版 7.7.13 及以后版本采用了系統(tǒng)API,舊方法無效但不會報(bào)錯
? ? ? ? if (isMiUIV7OrAbove()) {
return AvailableRomType.ANDROID_NATIVE;
? ? ? ? }
if (isMiUIV6OrAbove()) {
return AvailableRomType.MIUI;
? ? ? ? }
if (isFlymeV4OrAbove()) {
return AvailableRomType.FLYME;
? ? ? ? }
if (isAndroidMOrAbove()) {
return AvailableRomType.ANDROID_NATIVE;
? ? ? ? }
return AvailableRomType.NA;
? ? }
//Flyme V4的displayId格式為 [Flyme OS 4.x.x.xA]
//Flyme V5的displayId格式為 [Flyme 5.x.x.x beta]
? ? private static boolean isFlymeV4OrAbove() {
String displayId = Build.DISPLAY;
? ? ? ? if (!TextUtils.isEmpty(displayId) && displayId.contains("Flyme")) {
String[] displayIdArray = displayId.split(" ");
? ? ? ? ? ? for (String temp : displayIdArray) {
//版本號4以上帘饶,形如4.x.
? ? ? ? ? ? ? ? if (temp.matches("^[4-9]\\.(\\d+\\.)+\\S*")) {
return true;
? ? ? ? ? ? ? ? }
}
}
return false;
? ? }
//Android Api 23以上
? ? private static boolean isAndroidMOrAbove() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
? ? }
private static final StringKEY_MIUI_VERSION_CODE ="ro.miui.ui.version.code";
? ? private static boolean isMiUIV6OrAbove() {
try {
final Properties properties =new Properties();
? ? ? ? ? ? properties.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));
? ? ? ? ? ? String uiCode = properties.getProperty(KEY_MIUI_VERSION_CODE, null);
? ? ? ? ? ? if (uiCode !=null) {
int code = Integer.parseInt(uiCode);
? ? ? ? ? ? ? ? return code >=4;
? ? ? ? ? ? }else {
return false;
? ? ? ? ? ? }
}catch (final Exceptione) {
return false;
? ? ? ? }
}
static boolean isMiUIV7OrAbove() {
try {
final Properties properties =new Properties();
? ? ? ? ? ? properties.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));
? ? ? ? ? ? String uiCode = properties.getProperty(KEY_MIUI_VERSION_CODE, null);
? ? ? ? ? ? if (uiCode !=null) {
int code = Integer.parseInt(uiCode);
? ? ? ? ? ? ? ? return code >=5;
? ? ? ? ? ? }else {
return false;
? ? ? ? ? ? }
}catch (final Exceptione) {
return false;
? ? ? ? }
}
/**
*? 修改狀態(tài)欄文字顏色哑诊,這里小米,魅族區(qū)別對待及刻。
*/
? ? public static void setLightStatusBar(final Activityactivity, final boolean dark) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
switch (StatusBarUtil.getLightStatusBarAvailableRomType()) {
case StatusBarUtil.AvailableRomType.MIUI:
MIUISetStatusBarLightMode(activity, dark);
break;
? ? ? ? ? ? ? ? case StatusBarUtil.AvailableRomType.FLYME:
setFlymeLightStatusBar(activity, dark);
break;
? ? ? ? ? ? ? ? case StatusBarUtil.AvailableRomType.ANDROID_NATIVE:
setAndroidNativeLightStatusBar(activity, dark);
break;
? ? ? ? ? ? }
}
}
public static boolean MIUISetStatusBarLightMode(Activityactivity, boolean dark) {
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 (dark) {
extraFlagField.invoke(window, darkModeFlag, darkModeFlag);//狀態(tài)欄透明且黑色字體
? ? ? ? ? ? ? ? }else {
extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字體
? ? ? ? ? ? ? ? }
result =true;
? ? ? ? ? ? ? ? if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && StatusBarUtil.isMiUIV7OrAbove()) {
//開發(fā)版 7.7.13 及以后版本采用了系統(tǒng)API镀裤,舊方法無效但不會報(bào)錯,所以兩個方式都要加上
? ? ? ? ? ? ? ? ? ? if (dark) {
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_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
? ? ? ? ? ? ? ? ? ? }
}
}catch (Exceptione) {
}
}
return result;
? ? }
private static boolean setFlymeLightStatusBar(Activityactivity, boolean dark) {
boolean result =false;
? ? ? ? if (activity !=null) {
try {
WindowManager.LayoutParams lp =activity.getWindow().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 (dark) {
value |= bit;
? ? ? ? ? ? ? ? }else {
value &= ~bit;
? ? ? ? ? ? ? ? }
meizuFlags.setInt(lp, value);
? ? ? ? ? ? ? ? activity.getWindow().setAttributes(lp);
? ? ? ? ? ? ? ? result =true;
? ? ? ? ? ? }catch (Exceptione) {
}
}
return result;
? ? }
public static void setAndroidNativeLightStatusBar(Activityactivity, boolean dark) {
View decor =activity.getWindow().getDecorView();
? ? ? ? if (dark) {
decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
? ? ? ? }else {
decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
? ? ? ? }
}
/**
* 隱藏狀態(tài)欄
* 發(fā)現(xiàn)沒啥卵用
? ? * @param activity
? ? */
? ? @TargetApi(19)
public static void transparencyBar(Activityactivity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window =activity.getWindow();
? ? ? ? ? ? window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
? ? ? ? ? ? window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
? ? ? ? ? ? window.setStatusBarColor(Color.TRANSPARENT);
? ? ? ? ? ? window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
? ? ? ? ? ? ? ? ? ? | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
? ? ? ? }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);
? ? ? ? }
}
/**
* 修改狀態(tài)欄顏色缴饭,支持4.4以上版本
? ? * @param activity
? ? * @param colorId
? ? */
? ? public static void setStatusBarColor(Activityactivity, int colorId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window =activity.getWindow();
? ? ? ? ? ? window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
? ? ? ? ? ? window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
? ? ? ? ? ? window.setStatusBarColor(activity.getResources().getColor(colorId));
? ? ? ? }
}
/**
* 獲取狀態(tài)欄高度
? ? * @param context
? ? * @return
? ? */
? ? public static int getStatusBarHeight(Contextcontext) {
Class c =null;
? ? ? ? Object obj =null;
? ? ? ? Field field =null;
? ? ? ? int x =0, statusBarHeight =0;
? ? ? ? try {
c = Class.forName("com.android.internal.R$dimen");
? ? ? ? ? ? obj = c.newInstance();
? ? ? ? ? ? field = c.getField("status_bar_height");
? ? ? ? ? ? x = Integer.parseInt(field.get(obj).toString());
? ? ? ? ? ? statusBarHeight =context.getResources().getDimensionPixelSize(x);
? ? ? ? }catch (Exceptione1) {
e1.printStackTrace();
? ? ? ? }
return statusBarHeight;
? ? }
}