如您所知,我们可以通过以下代码从 xml 设置
windowLightStatusBar
。
<item name="android:windowLightStatusBar">true</item>
我需要以编程方式将此属性 true 更改为 false 或 false 更改为 true。有办法实现吗?
如果您想更改图标颜色,请设置此项
.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
并重置为默认设置
.setSystemUiVisibility(0);
但是如果你想改变状态栏的背景颜色使用这个
getWindow.setStatusBarColor(ContextCompat.getColor(activity,R.color.my_statusbar_color));
[API 26 更新]
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.insetsController?.setSystemBarsAppearance(WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS,
WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS)
} else {
@Suppress("DEPRECATION")
window.decorView.systemUiVisibility = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
} else {
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
}
}
并清除它
window.insetsController?.setSystemBarsAppearance(0, APPEARANCE_LIGHT_STATUS_BARS)
我相信这是正确的打开和关闭方法。
if (on) {
View view = getWindow().getDecorView();
view.setSystemUiVisibility(view.getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
} else {
View view = getWindow().getDecorView();
view.setSystemUiVisibility(view.getSystemUiVisibility() & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
Hidro 的答案几乎是正确的,但是 WindowInsetsControllerCompat 需要作为函数调用才能工作,否则它声称在我的情况下存在未解析的引用。
对于科特林:
WindowInsetsControllerCompat(window, yourView).isAppearanceLightStatusBars = true
对于 Java:
WindowInsetsControllerCompat(getWindow(), yourView).setAppearanceLightStatusBars(true)
要清除此属性,请使用以下代码:
window.clearFlags( View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
implementation "androidx.core:core-ktx:1.6.0"
活动
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
WindowInsetsControllerCompat(window, window.decorView).isAppearanceLightStatusBars = true
}
}
在 Android 8 和 Android 12 上测试。运行良好
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
getWindow().setStatusBarColor(Color.WHITE);
由于
View.setSystemUiVisibility()
已从 API 30 中弃用,取而代之的是新的 WindowInsetsController
API,因此 2021 年的答案现在是 WindowInsetsControllerCompat#setAppearanceLightStatusBars(boolean)
,向后兼容 API 23。必需 androidx.core:core:1.5.0-alpha05
或更高版本.
WindowInsetsControllerCompat.setAppearanceLightStatusBars(true)
仅这样做
它会使图标颜色变白
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
如果你想改变activity内的windowLightStatusBar
对于科特林
val windowInsetController = WindowCompat.getInsetsController(window,window.decorView)
windowInsetController.isAppearanceLightStatusBars = true
对于Java
WindowInsetsControllerCompat windowInsetController = WindowCompat.getInsetsController(getWindow(), getWindow().getDecorView());
windowInsetController.setAppearanceLightStatusBars(true);
如果您想更改片段内的 windowLightStatusBar
对于科特林
val windowInsetController = WindowCompat.getInsetsController(requireActivity().window,requireActivity().window.decorView)
windowInsetController.isAppearanceLightStatusBars = true
对于Java
WindowInsetsControllerCompat windowInsetController = WindowCompat.getInsetsController(requireActivity().getWindow(), requireActivity().getWindow().getDecorView());
windowInsetController.setAppearanceLightStatusBars(true);