[英]Light text in dark status bar is not working in Android 11
I'm changing status bar color and text between fragments/activities.我正在更改片段/活动之间的状态栏颜色和文本。 For black status bar I need white text and viceversa.
对于黑色状态栏,我需要白色文本,反之亦然。 Due to api deprecation on Android 11 I am using this:
由于 Android 11 上的 api 弃用,我正在使用这个:
Dark status bar with light text:带有浅色文本的深色状态栏:
Window window = getWindow();
if (mDefaultStatusBarColor == null) {
mDefaultStatusBarColor = window.getStatusBarColor();
}
window.setStatusBarColor(ContextCompat.getColor(this, R.color.black));
// Sets status text light
View decorView = window.getDecorView();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowInsetsController wic = decorView.getWindowInsetsController();
wic.setSystemBarsAppearance(0, APPEARANCE_LIGHT_STATUS_BARS);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
decorView.setSystemUiVisibility(decorView.getSystemUiVisibility() & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
Changing to light status bar with dark text:更改为带有深色文本的浅色状态栏:
Window window = getWindow();
// Giving the status bar its default color.
if (mDefaultStatusBarColor != null) {
window.setStatusBarColor(mDefaultStatusBarColor);
}
// Sets status text dark
View decorView = window.getDecorView();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowInsetsController wic = decorView.getWindowInsetsController();
wic.setSystemBarsAppearance(APPEARANCE_LIGHT_STATUS_BARS, APPEARANCE_LIGHT_STATUS_BARS);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
But the first case (dark status bar with light text) is not working properly because text is still dark.但是第一种情况(带有浅色文本的深色状态栏)无法正常工作,因为文本仍然是深色的。
Did I miss to add something?我错过了添加什么吗?
since SetSystemUIVisibilty was deprecated in API level 30, use WindowInsetsController instead:由于SetSystemUIVisibilty在 API 级别 30 中已弃用,请改用WindowInsetsController :
final WindowInsetsController insetsController = getWindow().getInsetsController();
int systemBarsAppearance = insetsController.getSystemBarsAppearance();
// Sets status text light
insetsController.setSystemBarsAppearance(systemBarsAppearance, ~WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS);
// Sets status bar dark
getWindow().setStatusBarColor(0xff000000);
All the best!一切顺利!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.