正如您所知道的,我们可以通过以下代码从xml中设置windowLightStatusBar
。
<item name="android:windowLightStatusBar">true</item>
我需要通过编程将此属性更改为true或false。有办法实现吗?
正如您所知道的,我们可以通过以下代码从xml中设置windowLightStatusBar
。
<item name="android:windowLightStatusBar">true</item>
我需要通过编程将此属性更改为true或false。有办法实现吗?
如果您想更改图标颜色,请设置此项
.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);
}
WindowInsetsController
进行相同操作的方法。 - qwertyfingerWindowInsetsControllerCompat(window, yourView).isAppearanceLightStatusBars = true
对于Java:
WindowInsetsControllerCompat(getWindow(), yourView).setAppearanceLightStatusBars(true)
yourView
可以直接使用 WindowInsetsControllerCompat(window, window.decorView)
。 - David Miguelandroid:windowLightStatusBar
,那么这将不起作用。您需要调用已弃用的函数以禁用系统UI标志。 - Hsingchien ChengWindowInsetsControllerCompat(requireActivity().window, view).isAppearanceLightStatusBars = false
,在片段的onViewCreated()
中调用。在API 26...33上运行得非常好。 - SonOfaSleepimplementation "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,表现良好。
window.clearFlags( View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
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);
如果你想在活动中改变窗口的亮色状态栏
Kotlin语言
val windowInsetController = WindowCompat.getInsetsController(window,window.decorView)
windowInsetController.isAppearanceLightStatusBars = true
针对Java
WindowInsetsControllerCompat windowInsetController = WindowCompat.getInsetsController(getWindow(), getWindow().getDecorView());
windowInsetController.setAppearanceLightStatusBars(true);
如果您想在片段中更改windowLightStatusBar
对于Kotlin
val windowInsetController = WindowCompat.getInsetsController(requireActivity().window,requireActivit().window.decorView)
windowInsetController.isAppearanceLightStatusBars = true
针对Java
WindowInsetsControllerCompat windowInsetController = WindowCompat.getInsetsController(requireActivity().getWindow(), requireActivity().getWindow().getDecorView());
windowInsetController.setAppearanceLightStatusBars(true);