如何更改 Android 中的状态栏颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22192291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the status bar color in Android?
提问by codercat
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
我希望状态栏颜色与导航栏颜色相同
采纳答案by codercat
Update:
更新:
Lollipop:
棒糖:
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 Material Design Guidelines
.
Android Lollipop 带来了更改应用程序中状态栏颜色的功能,以获得更加身临其境的用户体验并与 Google 的Material Design Guidelines
.
Here is how you can change the color of the status bar using the new window.setStatusBarColor
method introduced in API level 21
.
下面介绍了如何使用 中window.setStatusBarColor
引入的新方法更改状态栏的颜色API level 21
。
Changing the color of status bar also requires setting two additional flags on the Window; you need to add the FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
flag and clear the FLAG_TRANSLUCENT_STATUS
flag.
更改状态栏的颜色还需要在窗口上设置两个额外的标志;您需要添加FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
标志并清除FLAG_TRANSLUCENT_STATUS
标志。
Working Code:
工作代码:
import android.view.Window;
...
...
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 博客 - appcompat v21:适用于棒棒糖之前设备的材料设计!
The transitionName
for the view background will be android:status:background
.
在transitionName
该视图背景会android:status:background
。
回答by Giorgio
Android 5.0 Lollipop introduced Material Design theme which automatically colors the status bar based on the colorPrimaryDark
value of the theme.
Android 5.0 Lollipop 引入了 Material Design 主题,可根据colorPrimaryDark
主题值自动为状态栏着色。
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
由于库 support-v7-appcompat 从版本 21 开始,这在设备 pre-lollipop 上得到支持。来自 Chris Banes 的关于支持 appcompat v21 的博客文章
Read more about the Material Theme on the official Android Developers website
回答by Niels
Place this is your values-v21/styles.xml, to enable this on Lollipop:
将其放置在您的 values-v21/styles.xml 中,以在 Lollipop 上启用此功能:
<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>
回答by itzhar
this is very easy way to do this without any Library: if the OS version is not supported - under kitkat - so nothing happend. i do this steps:
这是在没有任何库的情况下执行此操作的非常简单的方法:如果不支持操作系统版本 - 在 kitkat 下 - 那么什么也没有发生。我执行以下步骤:
- in my xml i added to the top this View:
- 在我的 xml 中,我将此视图添加到顶部:
<View android:id="@+id/statusBarBackground" android:layout_width="match_parent" android:layout_height="wrap_content" />
<View android:id="@+id/statusBarBackground" android:layout_width="match_parent" android:layout_height="wrap_content" />
then i made this method:
然后我做了这个方法:
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:
您还需要这两种方法来获取操作栏和状态栏高度:
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:
那么你唯一需要的是这一行来设置状态栏颜色:
setStatusBarColor(findViewById(R.id.statusBarBackground),getResources().getColor(android.R.color.white));
回答by Faisal Shaikh
As @Niels said you have to place in values-v21/styles.xml:
正如@Niels 所说,您必须放入 values-v21/styles.xml:
<item name="android:statusBarColor">@color/black</item>
But add tools:targetApi="lollipop"
if you want single styles.xml, like:
但是tools:targetApi="lollipop"
如果你想要单个styles.xml,请添加,例如:
<item name="android:statusBarColor" tools:targetApi="lollipop">@color/black</item>
回答by Moti Bartov
Well, Izhar solution was OK but, personally, I am trying to avoid from code that looks as this:
好吧,Izhar 解决方案还可以,但就我个人而言,我试图避免使用看起来像这样的代码:
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:
同样,我也不喜欢重复代码。在您的回答中,我必须在所有活动中添加这样的代码行:
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 创建一个布局
<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:
使用包含 main_activity.xml 将此布局添加到您的活动布局:
<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:
对于工具栏,添加上边距属性:
<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:
样式-v19.xml,v21:
<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 上,添加 Toolbar topMargin 的值和 statusBarHeight: dimens.xml 的高度,小于 KitKat:
<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:
<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,如果需要,只需添加高程:
<resources>
<dimen name="statusBarElevation">4dp</dimen>
</resources>
This is the result for Jellybean KitKat and Lollipop:
这是 Jellybean KitKat 和 Lollipop 的结果:
回答by steve moretz
You can use this simple code:
您可以使用这个简单的代码:
One-liner in Kotlin:
Kotlin 中的单行:
window.statusBarColor = ContextCompat.getColor(this, R.color.colorName)
Original answer with Java & manual version check:
Java和手动版本检查的原始答案:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getWindow().setStatusBarColor(getResources().getColor(R.color.colorAccentDark_light, this.getTheme()));
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(getResources().getColor(R.color.colorAccentDark_light));
}
回答by lcompare
Just create a new theme in res/values/styles.xmlwhere you change the "colorPrimaryDark" which is the color of the status bar:
只需在res/values/styles.xml中创建一个新主题,您可以在其中更改“colorPrimaryDark”,即状态栏的颜色:
<style name="AppTheme.GrayStatusBar" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimaryDark">@color/colorGray</item>
</style>
And modify the activity theme in AndroidManifest.xmlto the one you want, on the next activity you can change the color back to the original one by selecting the original theme:
并将AndroidManifest.xml 中的活动主题修改为您想要的主题,在下一个活动中您可以通过选择原始主题将颜色更改回原始主题:
<activity
android:name=".LoginActivity"
android:theme="@style/AppTheme.GrayStatusBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This is how your res/values/colors.xmlshould look like:
你的res/values/colors.xml应该是这样的:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#c6d6f0</color>
<color name="colorGray">#757575</color>
</resources>
回答by sajad abbasi
You can change the status bar color with this function. works on android L means API 21 and higher and needs a color string such as "#ffffff"
.
您可以使用此功能更改状态栏颜色。适用于 android L 意味着 API 21 及更高版本,并且需要一个颜色字符串,例如"#ffffff"
.
private void changeStatusBarColor(String color){
if (Build.VERSION.SDK_INT >= 21) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.parseColor(color));
}
}
回答by Amin Keshavarzian
To change the color for above lolipop just add this to your styles.xml
要更改上述棒棒糖的颜色,只需将其添加到您的 styles.xml
<item name="android:statusBarColor">@color/statusBarColor</item>
but remember, if you want to have a light color for the status bar, add this line too
但请记住,如果您想为状态栏设置浅色,也请添加此行
<item name="android:windowLightStatusBar">true</item>