如何在Android中更改状态栏颜色

本文翻译自: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: 棒糖:

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 . 这是使用API level 21引入的新window.setStatusBarColor方法更改状态栏颜色的方法。

Changing the color of status bar also requires setting two additional flags on the Window; 更改状态栏的颜色还需要在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 Blog- appcompat v21:棒棒糖之前设备的材料设计!

在此处输入图片说明

The transitionName for the view background will be android:status:background . 视图背景的transitionName将为android:status:background


#3楼

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值自动为状态栏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 这是支持设备预棒棒糖感谢库支持-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: 我这样做:

  1. 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" /> 

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

#5楼

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>

#6楼

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: 使用include 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: styles-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上,添加工具栏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的结果:

在此处输入图片说明

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值