改变Android状态栏字体颜色和实现沉浸式状态栏

来源:互联网 发布:apache垃圾回收机制 编辑:程序博客网 时间:2024/05/06 17:01

目前已知的改变Android状态栏字体颜色的方法只有3种情况下可以实现,分别是手机是MIUI系统、魅族手机以及Android6.0。改变颜色方法分别如下:

* 改变小米的状态栏字体颜色为黑色, 要求MIUI6以上lightStatusBar为真时表示黑色字体*/private void processMIUI(booleanlightStatusBar) {Class<? extends Window> clazz = window.getClass();try {int darkModeFlag;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);extraFlagField.invoke(window, lightStatusBar? darkModeFlag : 0, darkModeFlag);} catch (Exception ignored) {}}

/*** 改变魅族的状态栏字体为黑色,要求FlyMe4以上*/private void processFlyMe(booleanisLightStatusBar) {WindowManager.LayoutParams lp= window.getAttributes();try {Class<?> instance = Class.forName("android.view.WindowManager$LayoutParams");int value = instance.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON").getInt(lp);Field field = instance.getDeclaredField("meizuFlags");field.setAccessible(true);int origin = field.getInt(lp);if (isLightStatusBar) {field.set(lp, origin | value);} else {field.set(lp, (~value)& origin);}} catch (Exception ignored) {//}}

/*** 处理Lollipop以上* Lollipop可以设置为沉浸,不能设置字体颜色(所以白色背景会很丑)* M(API23)可以设定*/@TargetApi(Build.VERSION_CODES.LOLLIPOP)private void processLollipopAbove() {if (Build.VERSION.SDK_INT< Build.VERSION_CODES.LOLLIPOP) {return;}int flag = window.getDecorView().getSystemUiVisibility();if (lightStatusBar) {/*** see {@link <a href="https://developer.android.com/reference/android/R.attr.html#windowLightStatusBar"></a>}*/flag |= (WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS| View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);}if (transparentStatusBar) {//改变字体颜色flag |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN| View.SYSTEM_UI_FLAG_LAYOUT_STABLE;}window.getDecorView().setSystemUiVisibility(flag);window.setStatusBarColor(Color.TRANSPARENT);}



如何实现状态栏沉浸式可以通过sysembartint这个库,通过initwindow方法在setcontent之后执行可以实现沉浸式效果:

@TargetApi(19)    private void initWindow(){        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);            SystemBarTintManager tintManager = new SystemBarTintManager(this);            tintManager.setStatusBarTintColor(getResources().getColor(R.color.white));            tintManager.setStatusBarTintEnabled(true);            ViewGroup contentView = (ViewGroup) findViewById(R.id.rl_tittle);            contentView.setPadding(0,getStatusBarHeight<span style="font-size: 16pt; font-family: Consolas;">(this),0,0);</span>            processMIUI(true);        }    }
public static int getStatusBarHeight(Context context){   Class<?> c = null;   Object obj = null;   Field field = null;   String bar = "status_bar_height";   int height = 0;   try {      c = Class.forName("com.android.internal.R$dimen");      obj = c.newInstance();      field = c.getField(bar);      int x = Integer.parseInt(field.get(obj).toString());      height = context.getResources().getDimensionPixelSize(x);   } catch(Exception e1) {      KasLog.e(TAG, "get bar height fail :"+e1.toString());      height = 0;   }   KasLog.d(TAG, bar + "height= " + height);   height = Math.max(0, height);   return height;}

如果你发现了其他能设置状态栏字体颜色的方法请通过留言板告诉我,谢谢。

0 0