【问题标题】:Status bar icons and text tint in Android versions < 23Android 版本 < 23 中的状态栏图标和文本色调
【发布时间】:2020-01-03 01:46:22
【问题描述】:

Android 中状态栏的默认色调是白色(因此状态栏会变暗是某种原因): I've found 我可以在大于或等于 23 的 Android 版本中更改色调。 但后来我发现我设备上的几个应用程序(Android 5.1,API 22)使用黑色。他们是怎么做到的?

更新: 这就是我的意思: 其他一些App有浅色状态栏和黑色图标、时间标签等。

这是我的带有白色状态栏的应用示例:

我无法将 windowLightStatusBar 设置为 true 以使其看起来像 API

【问题讨论】:

  • setStatusBarColor 是在 21 中添加的,而不是 23 中。所以他们正在您的手机上使用它。
  • @GabeSechan setStatusBarColor 只能设置我的状态栏的颜色,不能设置图标的颜色。所以会有白色的状态栏,上面有白色的图标和文字(我现在有这种情况)。要打开深色,您必须使用 windowLightStatusBar (API 23)
  • 您将不得不显示一些屏幕截图,因为通过标准方式无法实现您所要求的 afaik。
  • @EugenPechanec。添加图片使其更清晰。
  • 标准方法是根据手机供应商提供的 [Android SDK 扩展] 进行编译。这会将您的应用程序仅限于具有该 SDK 扩展的设备。您可能希望关注比这更重要的问题。

标签: android


【解决方案1】:

对于 API 级别

Window window = this.getWindow();
View view = window.getDecorView();
setLightStatusBar(view, this);

然后创建一个函数 setLightStatusBar,它将在浅色背景上显示深色图标:

public static void setLightStatusBar(View view, Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            int flags = view.getSystemUiVisibility();
            flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
            view.setSystemUiVisibility(flags);

            // You can change the background color of the status bar here
            activity.getWindow().setStatusBarColor(Color.WHITE);
        }
    }

对于 API 级别 23 及更高级别,只需将其添加到您的样式中

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="android:Theme.Material.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="android:windowLightStatusBar">true</item>
        <item name="android:statusBarColor">@android:color/white</item>
    </style>

</resources>

【讨论】:

  • 文本和背景仍然是白色的。任何将文本更改为黑色的方法>
【解决方案2】:

您可以使用此方法在 kitkat 和棒棒糖及以上设备中为 statusBar 赋予自己的颜色

public void translucentStatusBar() {

            if(Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
                getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                        WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
                getWindow().setStatusBarColor(Color.TRANSPARENT);
            }

            View view = findViewById(R.id.vFakeStatusBar);
            view.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                    (int) getResources().getDimension(R.dimen.default_status_bar_height)));
        }

在上面的代码中,在高度为 24dp(默认)的状态栏中的 sn-p 视图。

【讨论】:

  • 感谢您的帮助,但我指的是文本、图标、电池时间等的颜色。而不是状态栏的颜色。
【解决方案3】:

来自this的回答,添加values-v23/style.xml

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

希望对你有帮助

【讨论】:

  • 这很好用,但你应该使用 API >= 23 来使用它。我刚刚在我的设备上发现了几个可以使用此功能的应用程序 (API=22)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-18
  • 2015-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多