在 Android O 上切换状态栏颜色的问题

arn*_*100 2 android android-theme android-6.0-marshmallow android-8.0-oreo

有些东西不起作用;我不知道这是模拟器的问题还是 Android Oreo 上的错误(我没有使用 Android O 的物理设备),但我无法像 Marshmallow 那样从深色状态栏主题切换到浅色状态栏主题。

样式.xml(来自api 23):

<resources>

    <style name="ThemeLight" parent="MyBaseTheme">
        <item name="android:windowBackground">?attr/colorPrimary</item>
        <item name="android:statusBarColor">?attr/colorPrimary</item>
        <item name="android:windowLightStatusBar">true</item>
    </style>


    <style name="ThemeDark" parent="MyBaseThemeDark">
        <item name="android:windowBackground">?attr/colorPrimary</item>
        <item name="android:statusBarColor">?attr/colorPrimary</item>
        <item name="android:windowLightStatusBar">false</item>
    </style>


</resources>
Run Code Online (Sandbox Code Playgroud)

这在 Android M 上运行良好,当我使用 更改主题时setTheme(),但在 Android O 中,一旦我切换到浅色主题,它就不再更改并且状态栏保持黑色(windowLightStatusBar = true)/:

Rik*_*kka 5

这一定是一个错误,我已将其报告给谷歌(https://issuetracker.google.com/issues/65883460)。

我使用这些代码来临时解决问题。

// fix windowLightStatusBar not changed after applyStyle on Android O
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    final TypedArray a = obtainStyledAttributes(new int[]{android.R.attr.windowLightStatusBar});
    final boolean windowLightStatusBar = a.getBoolean(0, false);
    a.recycle();
    int flag = getWindow().getDecorView().getSystemUiVisibility();
    if (windowLightStatusBar) {
        flag |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
    } else {
        flag &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
    }
    getWindow().getDecorView().setSystemUiVisibility(flag);
}
Run Code Online (Sandbox Code Playgroud)