69.android 简单的沉浸式状态栏,设置状态栏颜色,以及给小米手机设置状态栏文字颜色

//第一种 简单的方式,直接把状态栏顶掉。为透明色。

 

//在Activity里使用:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
    int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Window window = getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.flags |= flagTranslucentNavigation;
        window.setAttributes(attributes);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
    } else {
        Window window = getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.flags |= flagTranslucentStatus | flagTranslucentNavigation;
        window.setAttributes(attributes);
    }
}

//第二种,给状态栏设置颜色,小米手机可设置状态栏文字颜色。

 //设置状态栏颜色在Activity里使用:

//因为不是所有的系统都可以设置颜色的,在4.4以下就不可以。。有的说4.1,所以在设置的时候要检查一下系统版本是否是4.1以上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.setStatusBarColor(getResources().getColor(R.color.Red));
}

//------------------------------------------------以下是小米手机给状态栏设置字体颜色------------------------------------------------
//如果你的小米手机是6.0以上的系统,那就需要加上:

this.getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
//小米手机设置状态栏字体颜色为黑色
private void SetStatusBarLightMode() {
    if (this.getWindow() != null) {
        Class clazz = this.getWindow().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);
            extraFlagField.invoke(this.getWindow(), darkModeFlag, darkModeFlag);//状态栏透明且黑色字体
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

//--------------------------------------------------------------------完---------------------------------------------------------------------------

猜你喜欢

转载自blog.csdn.net/weixin_42061754/article/details/82837755