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

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

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


当前回答

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

对于黑暗模式:

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

其他回答

还有一个解决方案:

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

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

一个非常非常古老的问题。但是对于那些想要改变状态栏颜色从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 > < /项目

<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

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

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