【问题标题】:Android Status bar color on different API Level不同API级别的Android状态栏颜色
【发布时间】:2019-03-11 19:02:08
【问题描述】:

我正在尝试设置状态栏颜色,但在使用不同的 api 级别时遇到了问题。

我想要深色图标颜色和黄色背景。我已经设置了版本样式,但它没有设置为 API 级别 21。请查看标记为红色的屏幕截图。

这里是代码

v23\styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowBackground">@color/colorPrimaryBackground</item>
    <item name="android:textColor">@color/colorPrimaryText</item>
    <item name="android:windowLightStatusBar">true</item>
</style>

默认样式.xml

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowBackground">@color/colorPrimaryBackground</item>
    <item name="android:textColor">@color/colorPrimaryText</item>
</style>

【问题讨论】:

    标签: android android-styles


    【解决方案1】:

    你想做的事情对 api 来说都是可能的>23 试试下面的代码:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
     View decor = getWindow().getDecorView();
     if (shouldChangeStatusBarTintToDark) {
        decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
     } else {
        // We want to change tint color to white again.
        // You can also record the flags in advance so that you can turn UI back completely if
        // you have set other flags before, such as translucent or full screen.
        decor.setSystemUiVisibility(0);
     }
    }
    

    【讨论】:

    • 我明白了。所以我们只能更改图标颜色 api>23?你提供的代码会做什么?
    • 是的,您可以在 >23 内完成。我提供的代码将动态地执行您已经在执行的操作。唯一的区别是,对于小于 23 的 api,您可以定义您想要执行的行为或将其保留为 android 默认值。这将在运行时完成
    猜你喜欢
    • 2016-11-23
    • 1970-01-01
    • 2017-03-05
    • 1970-01-01
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    相关资源
    最近更新 更多