首先,它不是一个重复的如何改变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);

其他回答

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

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

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

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

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

我不能用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">

将colorPrimaryDark更改为您想要的颜色到res/values/styles.xml文件中

    <resources>
        <color name="colorPrimary">#800000</color>
        <color name="colorPrimaryDark">#303F9F</color> //This Line
        <color name="colorAccent">#FF4081</color>
        <color name="red">#FF0000</color>
        <color name="white">#FFFFFF</color>
       <color name="cream">#fffdd0</color>
       <color name="burgundy">#800000</color>
    </resources>

您可以使用此功能更改状态栏的颜色。适用于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));
    }
}

解决方法很简单,把下面的代码放到style.xml中

对于黑暗模式:

<item name="android:windowLightStatusBar">false</item>
<item name="android:statusBarColor">@color/black</item>