以编程方式设置窗口亮度状态栏属性

24

正如您所知道的,我们可以通过以下代码从xml中设置windowLightStatusBar

<item name="android:windowLightStatusBar">true</item>

我需要通过编程将此属性更改为true或false。有办法实现吗?

9个回答

42

如果您想更改图标颜色,请设置此项

.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)

如果我需要将它设置为false怎么办? - aligur
你的意思是false吗?如果你想要反转它,那就不要设置它。 - Elias Fazel
1
当我将 "windowLightStatusBar" 设置为 true 时,状态栏图标的颜色变为灰色。但如果我将其设置为 false,则变为白色。据我所知,从您的代码中可以看出,您使用 ".setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_‌​BAR);" 将其设置为 true。那么我如何通过编程将其设置为 false 呢? - aligur
2
.setSystemUiVisibility(0); 测试这个 - Elias Fazel
1
如果我想要在低于“M”版本中执行该操作怎么办? - Sagar
非常感谢您的回答!我花了几个小时才找到我需要的东西...谢谢! - Gabriella Angelova

22

我相信这是打开和关闭的正确方式。

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);
}

唯一在两个方向上始终有效的方法。但仅适用于API <30。不幸的是,在API 30+上找不到使用WindowInsetsController进行相同操作的方法。 - qwertyfinger

13
Hidro的答案几乎正确,但是WindowInsetsControllerCompat需要被调用为一个函数才能工作,否则在我的情况下它会声称存在一个未解决的引用。 对于Kotlin:
WindowInsetsControllerCompat(window, yourView).isAppearanceLightStatusBars = true

对于Java:

WindowInsetsControllerCompat(getWindow(), yourView).setAppearanceLightStatusBars(true)

3
yourView 可以直接使用 WindowInsetsControllerCompat(window, window.decorView) - David Miguel
@AliZarei,你能解释一下为什么它不起作用吗?即使使用非常旧的androidx.core库版本,它似乎对我来说都很好用。 - Tong Jing Yen
@TongJingYen 在将 isAppearanceLightStatusBars 设置为 false 后,状态栏图标仍然是黑色的。 - Ali Zarei
2
如果您在样式中设置了android:windowLightStatusBar,那么这将不起作用。您需要调用已弃用的函数以禁用系统UI标志。 - Hsingchien Cheng
对于Java:new WindowInsetsControllerCompat(window,window.getDecorView())。setAppearanceLightStatusBars(true); - Zack
已经将其工作起来了: WindowInsetsControllerCompat(requireActivity().window, view).isAppearanceLightStatusBars = false,在片段的onViewCreated()中调用。在API 26...33上运行得非常好。 - SonOfaSleep

7
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,表现良好。


7
清除该属性,请使用以下代码:

window.clearFlags( View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)

4
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
getWindow().setStatusBarColor(Color.WHITE);

3

无法解决setAppearanceLightStatusBars。 - Ali Zarei
这是一个实例方法,因此您需要先创建一个WindowInsetsControllerCompat实例。 - martian111
我正在使用androidx.core:core-ktx:1.6.0,但无法找到此方法。 - famfamfam

1

只需这样做
它会使图标颜色变白

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);

0

活动中

如果你想在活动中改变窗口的亮色状态栏

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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接