Android custom white status bar theme style

UI revision changed the theme to the status bar with black characters on a white background, as shown in the figure

Keywords: Theme style, SDK version number, model,

Manifest configuration file

<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">
Copy the code

The theme

<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>
Copy the code

Create values-v19 under res and place styles; <4.4 System API 20 System status bar immersion/transparency >; Create values-v20 and values-v9; Set the last windowTranslucentStatus to true when creating values-v21

<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> <! Status bar translucent --> <! --<item name="android:windowTranslucentStatus">true</item>--> </style>Copy the code

Create values-v23 under res and place styles; For 6.0 system status bar immersion/transparency

<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>Copy the code

To the baseAppActivity of a project, add the following code: call the setStatusBarDarkMode () method in the activity;

/** * Call this method in the activity, And above style23, updated to inherit 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(); }}} /* * Static field, obtain system version based on 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(); } /** * Set the status bar icon color style of Meizu phone * can be used to determine whether it is a Flyme user ** @param window needs to be set * @param dark Whether the status bar font and icon color is set to dark * @return 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 VERSION greater than 4.4 System 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; }Copy the code

Px %2 = dp; px%2 = dp;

<? The 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=" grow" 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>Copy the code

The successful running results are as follows: Test models: Huawei 8.0, 7.0, 6.0, Nokia 7.0, OPPO 6.1, and Mi series

Problems encountered: OPPO 5.1 system phones will be white background white text; Redmi 6.0 system phone with white background and white text (try Redmi 8.0 and Mi 5.0 is no problem); Huawei 5.1, Samsung 5.1.1 version is still only half changed white, the status bar is still white, does not affect viewing; Hammer 7.1.1 is unchanged;

For Redmi 6.0 the getStatusBarLightMode() code has been changed as follows

@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; }Copy the code

Tried changing style-21 to style-23, it didn’t work, tried 5.0 there

getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
Copy the code

It doesn’t work either

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);
Copy the code

Considering that 9.0 has been out, no further research, there has been a solution to welcome advice. Thank you ~