Android實現(xiàn)狀態(tài)欄白底黑字和侵入式狀態(tài)欄

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;

import java.lang.reflect.Field;

import java.lang.reflect.Method;

public class LightStatusBarUtils {

/**

* 修改狀態(tài)欄為全透明

? ? * @param activity

? ? */

? ? @TargetApi(19)

public static void transparencyBar(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);

? ? ? ? }

}

/**

* 修改狀態(tài)欄顏色,支持4.4以上版本

? ? * @param activity

? ? * @param colorId

? ? */

? ? public static void setStatusBarColor(Activity activity,int colorId) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

Window window = activity.getWindow();

? ? ? ? ? ? window.setStatusBarColor(activity.getResources().getColor(colorId));

? ? ? ? }

}

/**

*狀態(tài)欄亮色模式,設(shè)置狀態(tài)欄黑色文字、圖標,

* 適配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android

? ? * @param activity

? ? * @return 1:MIUUI 2:Flyme 3:android6.0

*/

? ? public static int StatusBarLightMode(Activity activity){

int result=0;

? ? ? ? if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

if(MIUISetStatusBarLightMode(activity, true)){

result=1;

? ? ? ? ? ? }else if(FlymeSetStatusBarLightMode(activity.getWindow(), true)){

result=2;

? ? ? ? ? ? }else 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);

? ? ? ? ? ? ? ? result=3;

? ? ? ? ? ? }

}

return result;

? ? }

/**

* 已知系統(tǒng)類型時,設(shè)置狀態(tài)欄黑色文字、圖標。

* 適配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android

? ? * @param activity

? ? * @param type 1:MIUUI 2:Flyme 3:android6.0

*/

? ? public static void StatusBarLightMode(Activity activity,int type){

if(type==1){

MIUISetStatusBarLightMode(activity, true);

? ? ? ? }else if(type==2){

FlymeSetStatusBarLightMode(activity.getWindow(), true);

? ? ? ? }else if(type==3){

activity.getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

? ? ? ? }

}

/**

* 狀態(tài)欄暗色模式,清除MIUI、flyme或6.0以上版本狀態(tài)欄黑色文字、圖標

*/

? ? public static void StatusBarDarkMode(Activity activity,int type){

if(type==1){

MIUISetStatusBarLightMode(activity, false);

? ? ? ? }else if(type==2){

FlymeSetStatusBarLightMode(activity.getWindow(), false);

? ? ? ? }else if(type==3){

activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);

? ? ? ? }

}

/**

* 設(shè)置狀態(tài)欄圖標為深色和魅族特定的文字風(fēng)格

* 可以用來判斷是否為Flyme用戶

? ? * @param window 需要設(shè)置的窗口

? ? * @param dark 是否把狀態(tài)欄文字及圖標顏色設(shè)置為深色

? ? * @return? boolean 成功執(zhí)行返回true

*

*/

? ? public static boolean FlymeSetStatusBarLightMode(Window window, boolean dark) {

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 (dark) {

value |= bit;

? ? ? ? ? ? ? ? }else {

value &= ~bit;

? ? ? ? ? ? ? ? }

meizuFlags.setInt(lp, value);

? ? ? ? ? ? ? ? window.setAttributes(lp);

? ? ? ? ? ? ? ? result =true;

? ? ? ? ? ? }catch (Exception e) {

}

}

return result;

? ? }

/**

* 需要MIUIV6以上

? ? * @param activity

? ? * @param dark 是否把狀態(tài)欄文字及圖標顏色設(shè)置為深色

? ? * @return? boolean 成功執(zhí)行返回true

*

*/

? ? public static boolean MIUISetStatusBarLightMode(Activity activity, 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) {

//開發(fā)版 7.7.13 及以后版本采用了系統(tǒng)API,舊方法無效但不會報錯,所以兩個方式都要加上

? ? ? ? ? ? ? ? ? ? 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_VISIBLE);

? ? ? ? ? ? ? ? ? ? }

}

}catch (Exception e){

}

}

return result;

? ? }

}

調(diào)取方式:

StatusBarUtil.transparencyBar(this); //設(shè)置狀態(tài)欄全透明StatusBarUtil.StatusBarLightMode(this); //設(shè)置白底黑字

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容