如何改变状态栏的颜色,以匹配应用程序在棒棒糖? [安卓]

在最新的棒棒糖更新中,我注意到谷歌本地应用程序的状态栏颜色发生了变化,以便与你正在运行的应用程序的操作栏颜色相匹配。我看到它也在 Twitter 应用程序上,所以我猜不是只有谷歌才能做到这一点。

如果可能的话,有人知道怎么做吗?

189751 次浏览

To change status bar color use setStatusBarColor(int color). According the javadoc, we also need set some flags on the window.

Working snippet of code:

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


Keep in mind according Material Design guidelines status bar color and action bar color should be different:

  • ActionBar should use primary 500 color
  • StatusBar should use primary 700 color

Look at the screenshot below:

enter image description here

Another way to set the status bar color is through the style.xml.

To do that, create a style.xml file under res/values-v21 folder with this content:

<?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>

Edit: as pointed out in comments, when using AppCompat the code is different. In file res/values/style.xml use instead:

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

In android pre Lollipop devices you can do it from SystemBarTintManager If you are using android studio just add Systembartint lib in your gradle file.

dependencies {
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
...
}

Then in your activity

// 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));

To set the status bar color, create a style.xml file under res/values-v21 folder with this content:

<?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>

Just add this in you styles.xml. The colorPrimary is for the action bar and the colorPrimaryDark is for the status bar.

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:colorPrimary">@color/primary</item>
<item name="android:colorPrimaryDark">@color/primary_dark</item>
</style>

This picture from developer android explains more about color pallete. You can read more on this link.

enter image description here

Also if you want different status-bar color for different activity (fragments) you can do it with following steps (work on API 21 and above):

First create values21/style.xml and put following code:

 <style name="AIO" parent="AIOBase">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:windowContentTransitions">true</item>
</style>

Then define White|Dark themes in your values/style.xml like following:

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

Also don't forget to apply themes in your manifest.xml.

Add this line in style of v21 if you use two style.

  <item name="android:statusBarColor">#43434f</item>