以编程方式设置 windowlightstatusbar 属性

问题描述 投票:0回答:9

如您所知,我们可以通过以下代码从 xml 设置

windowLightStatusBar

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

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

android android-statusbar
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)

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

13
投票

Hidro 的答案几乎是正确的,但是 WindowInsetsControllerCompat 需要作为函数调用才能工作,否则它声称在我的情况下存在未解析的引用。

对于科特林:

WindowInsetsControllerCompat(window, yourView).isAppearanceLightStatusBars = true

对于 Java:

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

7
投票

要清除此属性,请使用以下代码:

window.clearFlags( View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)

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 上测试。运行良好


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

3
投票

由于

View.setSystemUiVisibility()
已从 API 30 中弃用,取而代之的是新的
WindowInsetsController
API,因此 2021 年的答案现在是
WindowInsetsControllerCompat#setAppearanceLightStatusBars(boolean)
,向后兼容 API 23。必需
androidx.core:core:1.5.0-alpha05
或更高版本.

WindowInsetsControllerCompat.setAppearanceLightStatusBars(true)


1
投票

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

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

1
投票

活动中

如果你想改变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);
© www.soinside.com 2019 - 2024. All rights reserved.