Android关于讲状态栏更改成白色背景,黑字

Android自定义白色状态栏主题风格

UI改版,将主题改为白底黑字的状态栏,如图所示

关键词:主题style、SDK版本号、机型、

清单配置文件

<application
    android:name=".LoveBabyApplication"
    android:allowBackup="true"
    android:icon="@mipmap/icon"
    android:label="@string/my_app_name"
    android:supportsRtl="true"
    android:theme="@style/MyAppTheme"
    tools:replace="android:label">
复制代码

主题

<style name="MyAppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="colorPrimary">@android:color/transparent</item>
    <item name="colorPrimaryDark">@android:color/transparent</item>
    <item name="colorAccent">@android:color/transparent</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowBackground">@null</item>
</style>
复制代码

在res文件夹下新建values-v19,放置styles; <4.4 系统 API 20 系统状态栏沉浸式/透明化>;新建values-v20同values-v9;新建values-v21时将最后的windowTranslucentStatus设置为true

<style name="MyAppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="colorPrimary">@android:color/transparent</item>
    <item name="colorPrimaryDark">@android:color/transparent</item>
    <item name="colorAccent">@android:color/transparent</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowBackground">@null</item>
    <!--状态栏半透明-->
    <!--<item name="android:windowTranslucentStatus">true</item>-->
</style>
复制代码

在res文件夹下新建values-v23,放置styles; 适配6.0系统状态栏沉浸式/透明化

<style name="MyAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@android:color/transparent</item>
    <item name="colorPrimaryDark">@android:color/transparent</item>
    <item name="colorAccent">@android:color/transparent</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>
复制代码

在项目的BaseAppCompatActivity中,添加以下代码:在activity中调用方法setStatusBarDarkMode();

 /**
 * 更新状态栏在activity中调用此方法,并将style23、以上更新为继承Theme.AppCompat.Light.DarkActionBar
 */
@SuppressLint("InlinedApi")
protected void setStatusBarDarkMode() {
    int type = getStatusBarLightMode();
    if (type == 1) {
        setMIUIStatusBarDarkMode();
    } else if (type == 2) {
        setMeiZuDarkMode(getWindow(), true);
    } else if (type == 3) {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    } else if (type == 4) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }
}

/**
 * 设置小米黑色状态栏字体
 */
@SuppressLint("PrivateApi")
private void setMIUIStatusBarDarkMode() {
    if (isMiUi) {
        Class<? extends Window> clazz = getWindow().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(getWindow(), darkModeFlag, darkModeFlag);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

/*
 * 静态域,获取系统版本是否基于MIUI
 */

static {
    try {
        @SuppressLint("PrivateApi") Class<?> sysClass = Class.forName("android.os.SystemProperties");
        Method getStringMethod = sysClass.getDeclaredMethod("get", String.class);
        String version = (String) getStringMethod.invoke(sysClass, "ro.miui.ui.version.name");
        isMiUi = version.compareTo("V6") >= 0 && Build.VERSION.SDK_INT < 24;
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * 设置魅族手机状态栏图标颜色风格
 * 可以用来判断是否为Flyme用户
 *
 * @param window 需要设置的窗口
 * @param dark   是否把状态栏字体及图标颜色设置为深色
 * @return boolean 成功执行返回true
 */
public static boolean setMeiZuDarkMode(Window window, boolean dark) {
    boolean result = false;
    if (Build.VERSION.SDK_INT >= 24) {
        return 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) {
            e.printStackTrace();
        }
    }
    return result;
}

@SuppressLint("InlinedApi")
private int getStatusBarLightMode() {
    int result = 0;
   // Android SDK版本号大于4.4系统
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        if (isMiUi) {
            result = 1;
        } else if (setMeiZuDarkMode(getWindow(), true)) {
            result = 2;
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            result = 3;
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            result = 4;
        }
    }
    return result;
}
复制代码

基本已经更改完成,因为项目旧时已引用了全屏显示,公用的title布局如下:px%2 = dp;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/zpx138dp"
android:background="@color/white"
android:id="@+id/ll_title"
android:orientation="horizontal">

<RelativeLayout
    android:id="@+id/back"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_marginBottom="1dp"
    android:layout_marginTop="@dimen/zpx50dp"
    android:paddingLeft="@dimen/zpx32dp"
    android:background="@drawable/btn_write"
    android:gravity="center"
    android:paddingRight="@dimen/zpx20dp">

    <ImageView
        android:id="@+id/iv_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@mipmap/back" />
</RelativeLayout>

<TextView
    android:id="@+id/title"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:layout_marginTop="@dimen/zpx50dp"
    android:text="成长"
    android:textColor="@color/text_color1"
    android:textSize="@dimen/text_size1"/>

<View
    android:id="@+id/view"
    android:layout_width="match_parent"
    android:layout_height="@dimen/zpx1dp"
    android:background="@color/view_line"
    android:layout_alignParentBottom="true"/>
</RelativeLayout>
复制代码

运行成功效果如下:测试机型:华为8.0、7.0、6.0版本、诺基亚7.0版本、OPPO 6.1版本、小米系列

诺基亚7.0系统

遇到问题:OPPO 5.1系统手机会白底白字;红米的6.0系统手机白底白字(试过红米8.0和小米5.0是没问题的);华为5.1、三星5.1.1版本 还是只改了一半白色,状态栏字还是白色,不影响观看;锤子7.1.1版本未改动;

针对红米6.0未改更改了一下getStatusBarLightMode()代码,如下

@SuppressLint("InlinedApi")
private int getStatusBarLightMode() {
    int result = 0;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        if (isMiUi && Build.VERSION.SDK_INT != 23) {
            result = 1;
        } else if (setMeiZuDarkMode(getWindow(), true)) {
            result = 2;
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//6.0
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            result = 3;
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            result = 4;
        }
    }
    return result;
}
复制代码

试过将style-21,改成style-23的样式不起作用,试过在5.0那里将

getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
复制代码

改成如下也不行

Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
复制代码

考虑到9.0都已经出了,未再深入研究,有已经解决的大神欢迎指教。谢谢~

转载于:https://juejin.im/post/5af3bf1751882567183f22d5

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值