android 以编程方式设置windowlightstatusbar属性

hiz5n14c  于 2023-01-24  发布在  Android
关注(0)|答案(9)|浏览(211)

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

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

我需要用编程的方法将这个属性从真到假或者从假到真。2有什么方法可以实现吗?

jjjwad0x

jjjwad0x1#

如果你想改变图标颜色,设置这个

.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_‌​BAR);

并重置为默认设置

.setSystemUiVisibility(0);

但是如果你想改变statusBar的背景颜色,可以使用

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

waxmsbnn2#

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

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

ecfsfe2w3#

Hidro的回答几乎是正确的,但是WindowInsetsControllerCompat需要作为函数调用才能工作,否则它会声称在我的情况下有一个未解决的引用。
对于Kotlin:

WindowInsetsControllerCompat(window, yourView).isAppearanceLightStatusBars = true

对于Java:

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

vom3gejh4#

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

window.clearFlags( View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
polhcujo

polhcujo5#

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

qlzsbp2j

qlzsbp2j6#

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

brqmpdu17#

由于View.setSystemUiVisibility()在API 30中已弃用,而支持新的WindowInsetsController API,因此2021年对此问题的答案现在是WindowInsetsControllerCompat#setAppearanceLightStatusBars(boolean),它向后兼容API 23。需要androidx.core:core:1.5.0-alpha05或更高版本。

WindowInsetsControllerCompat.setAppearanceLightStatusBars(true)
kuarbcqp

kuarbcqp8#

只做这个
它使图标颜色为白色

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

wkyowqbh9#

活动中

如果要更改活动中的windowLightStatusBar

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

相关问题