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

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

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


当前回答

若要更改状态栏的颜色,请转到 Res /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">#0000FF</item>
    </style>
</resources>

其他回答

更新:

棒棒糖:

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。

还有一个解决方案:

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);
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorPrimaryVariant">@color/colorPrimaryDark</item>
    <item name="colorOnPrimary">@color/white</item>
    <!-- Secondary brand color. -->
    <item name="colorOnSecondary">@color/black</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
</style>

注意:设置colorPrimaryVariant

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

只需在styles.xml文件中添加这些行

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- This is used for statusbar color. -->
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <!-- This is used for statusbar content color. If statusbarColor is light, use "true" otherwise use "false"-->
    <item name="android:windowLightStatusBar">false</item>
</style>