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

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

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


当前回答

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

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

其他回答

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

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)

若要更改状态栏的颜色,请转到 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>

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

还有一个解决方案:

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