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

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

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


当前回答

Java: 在活动的onCreate方法中使用此方法

Window window = this.getWindow();
window.setStatusBarColor(this.getResources().getColor(R.color.main_screen_bg_color));

科特林:

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

其他回答

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

对于黑暗模式:

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

一个非常非常古老的问题。但是对于那些想要改变状态栏颜色从ANDROID 5.0, API 21及以上根据主题暗和光甚至设备默认。 把这段代码放在你的活动super.onCreate(savedInstanceState)之后;setContentView(r.b ayout.activity_main);

int currentNightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
    switch (currentNightMode) {
        case Configuration.UI_MODE_NIGHT_NO:
            // Night mode is not active on device
            // For WHITE status bar Icons color to dark
            Window window = getWindow();
            View view = window.getDecorView();
            new WindowInsetsControllerCompat(window, view).setAppearanceLightStatusBars(true);
            break;
        case Configuration.UI_MODE_NIGHT_YES:
            // Night mode is active on device
            break;
    }

也符合你的风格。Xmlput这行 <项目名称= " android: statusBarColor " > @color / colorWhite > < /项目

如果你想在Android 4.4及以上版本上工作,试试这个。我参考了Harpreet的答案和这个链接。Android和透明的状态栏

首先,在Activity的onCreate方法中调用setStatusBarColored方法(我把它放在util类中)。我在这里用了一张图片,你可以用颜色来改变它。

public static void setStatusBarColored(Activity context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
    {
        Window w = context.getWindow();
        w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        int statusBarHeight = getStatusBarHeight(context);

        View view = new View(context);
        view.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        view.getLayoutParams().height = statusBarHeight;
        ((ViewGroup) w.getDecorView()).addView(view);
        view.setBackground(context.getResources().getDrawable(R.drawable.navibg));
    }
}

public static int getStatusBarHeight(Activity context) {
    int result = 0;
    int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        result = context.getResources().getDimensionPixelSize(resourceId);
    }
    return result;
}

之前:

后:

状态栏的颜色已经改变了,但是导航栏被切断了,所以我们需要在onCreate方法中设置导航栏的边距或偏移量。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, (int)(this.getResources().getDimension(R.dimen.navibar_height)));
        layoutParams.setMargins(0, Utils.getStatusBarHeight(this), 0, 0);

        this.findViewById(R.id.linear_navi).setLayoutParams(layoutParams);
    }

然后状态栏将是这样的。

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

这就是我在奇巧(KitKat)的做法,而且效果不错。

public static void setTaskBarColored(Activity context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        {
            Window w = context.getWindow();
            w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //status bar height
            int statusBarHeight = Utilities.getStatusBarHeight(context);

            View view = new View(context);
            view.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            view.getLayoutParams().height = statusBarHeight;
            ((ViewGroup) w.getDecorView()).addView(view);
            view.setBackgroundColor(context.getResources().getColor(R.color.colorPrimaryTaskBar));
        }
    }