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

其他回答

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

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

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

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

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

在kotlin中,我能够使用以下方法解决这个问题:

  window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
  window.statusBarColor = ContextCompat.getColor(this, android.R.color.transparent)

从要更改状态栏颜色的活动调用方法。

blackIconStatusBar(this, R.color.white);

方法定义

public static void blackIconStatusBar(Activity activity, int color) {

    activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
    activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    activity.getWindow().setStatusBarColor(ContextCompat.getColor(activity, color));
}

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