不推荐使用SYSTEM_UI_FLAG_LIGHT_STATUS_BAR和FLAG_TRANSLUCENT_STATUS [英] SYSTEM_UI_FLAG_LIGHT_STATUS_BAR and FLAG_TRANSLUCENT_STATUS is deprecated
问题描述
此代码已在API 30中弃用.您知道如何更新吗?
This code is deprecated in API 30. Any idea how to update this?
private fun setSystemBarLight(act: Activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val view: View = act.findViewById(android.R.id.content)
var flags: Int = view.systemUiVisibility
flags = flags or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
view.systemUiVisibility = flags
}
}
FLAG_TRANSLUCENT_STATUS 在这里也被弃用.我需要解决此警告的帮助.
FLAG_TRANSLUCENT_STATUS is also deprecated here. I need help fixing this warning.
private fun setSystemBarColor(act: Activity, color: String?) {
val window: Window = act.window
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
window.statusBarColor = Color.parseColor(color)
}
推荐答案
这些解决方案仅适用于API>23.
As suggested in the comment section, you can use WindowInsetsControllerCompat as follows.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Window window = getWindow();
View decorView = window.getDecorView();
WindowInsetsControllerCompat wic = new WindowInsetsControllerCompat(window, decorView);
wic.setAppearanceLightStatusBars(true); // true or false as desired.
// And then you can set any background color to the status bar.
window.setStatusBarColor(Color.WHITE);
}
旧答案:
在API级别30中 setSystemUiVisibility() 已弃用.因此,您应该使用 WindowInsetsController .以下代码将使状态栏变亮,这意味着状态栏中的文本将为黑色.
In API level 30 setSystemUiVisibility() has been deprecated. Hence you should use WindowInsetsController. The below code will make the status bar light, that means the text in the statut bar will be black.
Window window = getWindow();
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);
}
// set any light background color to the status bar. e.g. - white or light blue
window.setStatusBarColor(Color.WHITE);
要清除此问题,请使用
wic.setSystemBarsAppearance(0, APPEARANCE_LIGHT_STATUS_BARS);
这篇关于不推荐使用SYSTEM_UI_FLAG_LIGHT_STATUS_BAR和FLAG_TRANSLUCENT_STATUS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!