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

其他回答

你可以使用这段简单的代码:

Kotlin中的一行代码:

window.statusBarColor = ContextCompat.getColor(this, R.color.colorName)

原始答案与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));
}

更新:

棒棒糖:

public abstract void setStatusBarColor (int color)

在API级别21中添加

Android Lollipop能够改变应用程序中状态栏的颜色,以获得更沉浸式的用户体验,并与谷歌的材质设计指南相一致。

下面是如何使用新窗口更改状态栏的颜色。在API级别21中引入的setStatusBarColor方法。

改变状态栏的颜色还需要在窗口上设置两个额外的标志;你需要添加flag_draws_system_bar_background标志并清除FLAG_TRANSLUCENT_STATUS标志。

工作代码:

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

官方开发人员参考:setStatusBarColor(int)

例如:material-design-everywhere

Chris Banes博客- appcompat v21:前棒棒糖设备的材料设计!

视图背景的transitionName将是android:status:background。

如果你想通过编程方式改变状态栏的颜色(前提是设备有Android 5.0)。 这是一个简单的方法来改变statusBarColor从任何活动 和非常简单的方法,当不同的片段有不同的状态栏颜色。

 /**
 * @param colorId id of color
 * @param isStatusBarFontDark Light or Dark color
 */
fun updateStatusBarColor(@ColorRes colorId: Int, isStatusBarFontDark: Boolean = true) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        val window = window
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        window.statusBarColor = ContextCompat.getColor(this, colorId)
        setSystemBarTheme(isStatusBarFontDark)
    }
}

/** Changes the System Bar Theme.  */
@RequiresApi(api = Build.VERSION_CODES.M)
private fun setSystemBarTheme(isStatusBarFontDark: Boolean) {
    // Fetch the current flags.
    val lFlags = window.decorView.systemUiVisibility
    // Update the SystemUiVisibility depending on whether we want a Light or Dark theme.
    window.decorView.systemUiVisibility = if (isStatusBarFontDark) lFlags and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv() else lFlags or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
}

放置这是你的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>

在values/theme.xml中,添加命名为name="android:statusBarColor"的项。

 <resources xmlns:tools="http://schemas.android.com/tools">
        <style name="Theme.YourAppName" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
            ...
            ...
            ...
            <!-- Status bar color. -->
            <item name="android:statusBarColor" tools:targetApi="l">@color/purple_700</item>
        </style>
    </resources>