本文翻译自:How to change the status bar color in android
First of all it's not a duplicate as in How to change the background color of android status bar 首先,它不是重复的,如如何更改android状态栏的背景颜色
How do I change the status bar color which should be same as in navigation bar. 如何更改状态栏颜色,该颜色应与导航栏中的颜色相同。
I want the status bar color to be same as the navigation bar color 我希望状态栏颜色与导航栏颜色相同
#1楼
参考:https://stackoom.com/question/1V7EB/如何在Android中更改状态栏颜色
#2楼
Update: 更新:
Lollipop: 棒糖:
1 | public abstract void setStatusBarColor (int color) |
Added in API level 21 在API级别21中添加
Android Lollipop brought with it the ability to change the color of status bar in your app for a more immersive user experience and in tune with Google's
Here is how you can change the color of the status bar using the new
Changing the color of status bar also requires setting two additional flags on the Window; 更改状态栏的颜色还需要在Window上设置两个其他标志。 you need to add the
Working Code: 工作代码:
1 | import android.view.Window; |
... ...
1 2 3 4 5 6 7 8 9 10 | Window window = activity.getWindow(); // clear FLAG_TRANSLUCENT_STATUS flag: window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); // add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); // finally change the color window.setStatusBarColor(ContextCompat.getColor(activity,R.color.my_statusbar_color)); |
Offcial developer reference : setStatusBarColor(int) 官方开发人员参考: setStatusBarColor(int)
Example : material-design-everywhere 示例: 无处不在的材料设计
Chris Banes Blog- appcompat v21: material design for pre-Lollipop devices! Chris Banes Blog- appcompat v21:棒棒糖之前设备的材料设计!
The
#3楼
Android 5.0 Lollipop introduced Material Design theme which automatically colors the status bar based on the
This is supported on device pre-lollipop thanks to the library support-v7-appcompat starting from version 21. Blogpost about support appcompat v21 from Chris Banes 这是支持设备预棒棒糖感谢库支持-V7-程序兼容性起价版本21. 博文关于支持程序兼容性V21从克里斯巴内斯
Read more about the Material Theme on the official Android Developers website 在Android开发者官方网站上了解有关Material Theme的更多信息
#4楼
this is very easy way to do this without any Library: if the OS version is not supported - under kitkat - so nothing happend. 这是非常简单的方法,无需任何库即可:如果不支持操作系统版本(在kitkat下),则什么也没发生。 i do this steps: 我这样做:
- in my xml i added to the top this View: 在我的xml中,我将此视图添加到顶部:
1 <wyn><View android:id="@+id/statusBarBackground" android:layout_width="match_parent" android:layout_height="wrap_content" /></wyn>
then i made this method: 然后我做了这个方法:
1 2 3 4 5 6 7 8 9 10 11 12 | public void setStatusBarColor(View statusBar,int color){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window w = getWindow(); w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //status bar height int actionBarHeight = getActionBarHeight(); int statusBarHeight = getStatusBarHeight(); //action bar height statusBar.getLayoutParams().height = actionBarHeight + statusBarHeight; statusBar.setBackgroundColor(color); } } |
also you need those both methods to get action Bar & status bar height: 您也需要同时使用这两种方法来获得操作栏和状态栏的高度:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public int getActionBarHeight() { int actionBarHeight = 0; TypedValue tv = new TypedValue(); if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) { actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics()); } return actionBarHeight; } public int getStatusBarHeight() { int result = 0; int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { result = getResources().getDimensionPixelSize(resourceId); } return result; } |
then the only thing you need is this line to set status bar color: 然后,您唯一需要的是此行来设置状态栏颜色:
1 | setStatusBarColor(findViewById(R.id.statusBarBackground),getResources().getColor(android.R.color.white)); |
#5楼
Place this is your values-v21/styles.xml, to enable this on Lollipop: 将其放在您的values-v21 / styles.xml中,以在Lollipop上启用它:
1 2 3 4 5 6 7 8 9 | <resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light"> <item name="colorPrimary">@color/color_primary</item> <item name="colorPrimaryDark">@color/color_secondary</item> <item name="colorAccent">@color/color_accent</item> <item name="android:statusBarColor">@color/color_primary</item> </style> </resources> |
#6楼
Well, Izhar solution was OK but, personally, I am trying to avoid from code that looks as this: 好了,Izhar解决方案还可以,但是就我个人而言,我试图避免使用看起来像这样的代码:
1 2 3 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //Do what you need for this SDK }; |
As well, I don't like to duplicate code either. 同样,我也不喜欢重复代码。 In your answer I have to add such line of code in all Activities: 在您的答案中,我必须在所有活动中添加以下代码行:
1 | setStatusBarColor(findViewById(R.id.statusBarBackground),getResources().getColor(android.R.color.white)); |
So, I took Izhar solution and used XML to get the same result: Create a layout for the StatusBar status_bar.xml 因此,我采用了Izhar解决方案,并使用XML来获得相同的结果:为StatusBar status_bar.xml创建布局
1 2 3 4 5 | <View xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="@dimen/statusBarHeight" android:background="@color/primaryColorDark" android:elevation="@dimen/statusBarElevation"> |
Notice the height and elevation attributes, these will be set in values, values-v19, values-v21 further down. 注意高度和高程属性,这些属性将在更下方的值-v19,值-v21中设置。
Add this layout to your activities layout using include, main_activity.xml: 使用include main_activity.xml将此布局添加到您的活动布局中:
1 2 3 4 5 6 7 8 9 | <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/Black" > <include layout="@layout/status_bar"/> <include android:id="@+id/app_bar" layout="@layout/app_bar"/> //The rest of your layout </RelativeLayout> |
For the Toolbar, add top margin attribute: 对于工具栏,添加上边距属性:
1 2 3 4 5 6 7 8 9 10 11 | <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="?android:attr/actionBarSize" android:background="@color/primaryColor" app:theme="@style/MyCustomToolBarTheme" app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" android:elevation="@dimen/toolbarElevation" android:layout_marginTop="@dimen/appBarTopMargin" android:textDirection="ltr" android:layoutDirection="ltr"> |
In your appTheme style-v19.xml and styles-v21.xml, add the windowTranslucent attr: 在您的appTheme style-v19.xml和styles-v21.xml中,添加windowTranslucent属性:
styles-v19.xml, v21: styles-v19.xml,v21:
1 2 3 | <resources> <item name="android:windowTranslucentStatus">true</item> </resources> |
And finally, on your dimens, dimens-v19, dimens-v21, add the values for the Toolbar topMargin, and the height of the statusBarHeight: dimens.xml for less than KitKat: 最后,在您的dimens-v19,dimens-v21上,添加工具栏topMargin的值和statusBarHeight:dimens.xml的高度,使其小于KitKat:
1 2 3 4 5 | <resources> <dimen name="toolbarElevation">4dp</dimen> <dimen name="appBarTopMargin">0dp</dimen> <dimen name="statusBarHeight">0dp</dimen> </resources> |
The status bar height is always 24dp dimens-v19.xml for KitKat and above: 对于KitKat及更高版本,状态栏的高度始终为24dp dimens-v19.xml:
1 2 3 4 | <resources> <dimen name="statusBarHeight">24dp</dimen> <dimen name="appBarTopMargin">24dp</dimen> </resources> |
dimens-v21.xml for Lolipop, just add the elevation if needed: Lolipop的dimens-v21.xml,如果需要,只需添加高度:
1 2 3 | <resources> <dimen name="statusBarElevation">4dp</dimen> </resources> |
This is the result for Jellybean KitKat and Lollipop: 这是Jellybean KitKat和Lollipop的结果: