在 Android 上切换状态栏颜色的问题 O
Issue with toggling status bar color on Android O
有些东西不工作;不知道是模拟器的问题还是AndroidOreo的bug(我没有带AndroidO的实体设备),但是无法从暗状态切换到亮状态像 Marshmallow 中的酒吧主题。
styles.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>
这在 Android M 上工作正常,当我使用 setTheme()
更改主题时,但在 Android O 中,一旦我切换到浅色主题,它就不再更改并且状态栏保持黑色 (windowLightStatusBar = true) /:
这一定是一个错误,我已经向 google (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);
}
有些东西不工作;不知道是模拟器的问题还是AndroidOreo的bug(我没有带AndroidO的实体设备),但是无法从暗状态切换到亮状态像 Marshmallow 中的酒吧主题。
styles.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>
这在 Android M 上工作正常,当我使用 setTheme()
更改主题时,但在 Android O 中,一旦我切换到浅色主题,它就不再更改并且状态栏保持黑色 (windowLightStatusBar = true) /:
这一定是一个错误,我已经向 google (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);
}