使用 AppCompat ActionBarActivity 更改状态栏颜色 [英] Change status bar color with AppCompat ActionBarActivity

查看:28
本文介绍了使用 AppCompat ActionBarActivity 更改状态栏颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一项活动中,我使用 Palette 更改了工具栏的颜色.但是在使用 ActionBarActivity 的 5.0 设备上,status bar 颜色是我的活动主题中 colorPrimaryDark 的颜色,所以我有 2 种非常不同的颜色,它不好看.

In one of my Activities, I changed the Toolbar color using Palette. But on 5.0 devices using ActionBarActivity the status bar color is the color of my colorPrimaryDark in my activity theme so I have 2 very different colors and it does not look good.

我意识到在 5.0 中你可以使用 Window.setStatusBarColor()ActionBarActivity 没有这个.

I realize that in 5.0 you can use Window.setStatusBarColor() but ActionBarActivity does not have this.

所以我的问题是在 5.0 中如何使用 ActionBarActivity 更改状态栏颜色?

so my question is in 5.0 how can I change the status bar color with ActionBarActivity?

推荐答案

我不确定我是否理解问题.

I'm not sure I understand the problem.

我想以编程方式更改状态栏颜色(并提供设备具有 Android 5.0)然后您可以使用 Window.setStatusBarColor().Activity 派生自 Activity 还是 ActionBarActivity 应该没有区别.

I you want to change the status bar color programmatically (and provided the device has Android 5.0) then you can use Window.setStatusBarColor(). It shouldn't make a difference whether the activity is derived from Activity or ActionBarActivity.

试着做:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.setStatusBarColor(Color.BLUE);
}

刚刚用 ActionBarActivity 测试了这个,它工作正常.

Just tested this with ActionBarActivity and it works alright.

注意:如果您的 values-v21 样式文件已经设置了 FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS 标志,则无需通过以下方式设置它:

Note: Setting the FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag programmatically is not necessary if your values-v21 styles file has it set already, via:

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

这篇关于使用 AppCompat ActionBarActivity 更改状态栏颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆