首先,它不是一个重复的如何改变android状态栏的背景颜色

我如何改变状态栏的颜色,这应该是相同的导航栏。

我希望状态栏的颜色与导航栏的颜色相同


当前回答

Android 5.0棒棒糖引入了材质设计主题,根据主题的colorPrimaryDark值自动为状态栏上色。

realdognose注意:在材质设计库中,它将是colorPrimaryVariant

由于从版本21开始的support-v7-appcompat库,设备pre-lollipop支持这一点。Chris Banes关于支持appcompat v21的博文

在官方Android开发者网站上阅读更多关于材质主题的信息

其他回答

还有一个解决方案:

final View decorView = w.getDecorView();
View view = new View(BaseControllerActivity.this);
final int statusBarHeight = UiUtil.getStatusBarHeight(ContextHolder.get());
view.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight));
view.setBackgroundColor(colorValue);
((ViewGroup)decorView).addView(view);

好吧,Izhar的解决方案是可以的,但就我个人而言,我尽量避免这样的代码:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//Do what you need for this SDK
};

但我也不喜欢重复代码。在你的回答中,我必须在所有活动中添加如下一行代码:

setStatusBarColor(findViewById(R.id.statusBarBackground),getResources().getColor(android.R.color.white));

因此,我采用了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">

注意height和elevation属性,这些设置在values中,values-v19, values-v21再往下。

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

对于工具栏,添加top margin属性:

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

< / android.support.v7.widget.Toolbar >

在你的appTheme样式-v19.xml和样式-v21.xml中,添加window半透明attr:

styles-v19.xml v21:

<resources>
<item name="android:windowTranslucentStatus">true</item>
</resources>

最后,在你的维度上,dimensions -v19, dimensions -v21,添加工具栏topMargin的值,以及statusBarHeight的高度: 小于KitKat的:

<resources>
<dimen name="toolbarElevation">4dp</dimen>
<dimen name="appBarTopMargin">0dp</dimen>
<dimen name="statusBarHeight">0dp</dimen>
</resources>

状态栏的高度总是24dp 维-v19.xml的奇巧和以上:

<resources>
<dimen name="statusBarHeight">24dp</dimen>
<dimen name="appBarTopMargin">24dp</dimen>
</resources>

如果需要,只需要添加抬高:

<resources>
<dimen name="statusBarElevation">4dp</dimen>
</resources>

这是Jellybean KitKat和Lollipop的结果:

我有这样的需求:通过编程方式改变状态栏的颜色,使其保持透明,以允许导航抽屉绘制自己与透明的状态栏重叠。

我不能用API来做这个

getWindow().setStatusBarColor(ContextCompat.getColor(activity ,R.color.my_statusbar_color)

如果你在stack overflow中检查这一行代码之前的所有人,将状态栏的透明度设置为solid with

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)

我能够像这样管理状态栏的颜色和透明度:

Android 4: there's not much you can do, because you can't manage status bar color from the API ... the only thing you can do is to set the status bar as translucent and move a colored element of you UI under the status bar. To do this you need to play with android:fitsSystemWindows="false" in your main layout. This allows you to draw you layout under the status bar. Then you need to play with some padding with the top of your main layout. Android 5 and above: you have to define a style with <item name="android:windowDrawsSystemBarBackgrounds">true</item> <item name="android:statusBarColor">@android:color/transparent</item> this allows the navigation drawer to overlap the status bar. Then to change the color keeping the status bar transparent you have to set the status bar color with drawerLayout.setStatusBarBackgroundColor(ContextCompat.getColor(activity, R.color.my_statusbar_color)) where drawerLayout is defined like this <android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true">

放置这是你的values-v21/styles.xml,在棒棒糖上启用它:

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

只需在res/values/styles.xml中创建一个新主题,在其中更改状态栏的颜色“colorPrimaryDark”:

<style name="AppTheme.GrayStatusBar" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimaryDark">@color/colorGray</item>
</style>

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

这就是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>