在新的棒棒糖更新中,我注意到,使用原生Google应用,状态栏的颜色会更改为与您正在运行的应用上的操作栏相匹配。我看到它也在Twitter应用程序上,所以我猜测它不仅仅是Google可以做到的。
有可能有人知道如何做到这一点吗?
答案 0 :(得分:217)
要更改状态栏颜色,请使用setStatusBarColor(int color)。 根据javadoc,我们还需要在窗口上设置一些标志。
代码片段:
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(ContextCompat.getColor(activity, R.color.example_color));
请记住according Material Design guidelines状态栏颜色和操作栏颜色应该不同:
请看下面的截图:
答案 1 :(得分:40)
设置状态栏颜色的另一种方法是通过 style.xml 。
为此,请在 res / values-v21 文件夹下使用此内容创建 style.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="android:Theme.Material">
<!-- darker variant for the status bar and contextual app bars -->
<item name="android:colorPrimaryDark">@color/blue_dark</item>
</style>
</resources>
编辑:正如评论中所指出的,当使用AppCompat时,代码是不同的。在文件 res / values / style.xml 中使用:
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
<!-- Set AppCompat’s color theming attrs -->
<item name="colorPrimary">@color/my_awesome_red</item>
<item name="colorPrimaryDark">@color/my_awesome_darker_red</item>
<!-- Other attributes -->
</style>
答案 2 :(得分:20)
要设置状态栏颜色,请使用以下内容在res / values-v21文件夹下创建style.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppBaseTheme" parent="AppTheme">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@color/blue</item>
</style>
</resources>
答案 3 :(得分:3)
此外,如果您希望针对不同的活动(fragments)使用不同的status-bar
颜色,则可以执行以下步骤(处理API 21及更高版本):
首先创建values21/style.xml
并输入以下代码:
<style name="AIO" parent="AIOBase">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:windowContentTransitions">true</item>
</style>
然后在values/style.xml
中定义白色|黑暗主题,如下所示:
<style name="AIOBase" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/color_primary</item>
<item name="colorPrimaryDark">@color/color_primary_dark</item>
<item name="colorAccent">@color/color_accent</item>
<item name="android:textColorPrimary">@android:color/black</item>
<item name="android:statusBarColor" tools:targetApi="lollipop">@color/color_primary_dark
</item>
<item name="android:textColor">@color/gray_darkest</item>
<item name="android:windowBackground">@color/default_bg</item>
<item name="android:colorBackground">@color/default_bg</item>
</style>
<style name="AIO" parent="AIOBase" />
<style name="AIO.Dark" parent="AIOBase">
<item name="android:statusBarColor" tools:targetApi="lollipop">#171717
</item>
</style>
<style name="AIO.White" parent="AIOBase">
<item name="android:statusBarColor" tools:targetApi="lollipop">#bdbdbd
</item>
</style>
另外,请勿忘记在manifest.xml
中使用主题。
答案 4 :(得分:2)
在android pre Lollipop设备中,您可以从 SystemBarTintManager 执行此操作 如果你正在使用android studio,只需在你的gradle文件中添加Systembartint lib。
dependencies {
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
...
}
然后在您的活动中
// create manager instance after the content view is set
SystemBarTintManager mTintManager = new SystemBarTintManager(this);
// enable status bar tint
mTintManager.setStatusBarTintEnabled(true);
mTintManager.setTintColor(getResources().getColor(R.color.blue));
答案 5 :(得分:2)
如果使用两种样式,请以v21的样式添加此行。
<item name="android:statusBarColor">#43434f</item>