【问题标题】:Android theme will not update status bar colorAndroid 主题不会更新状态栏颜色
【发布时间】:2020-04-26 15:53:42
【问题描述】:

我正在努力让 Android 更新我的状态栏颜色。我在 Xamarin.Android 中使用 AppCompatActivity

我的values/styles.xml 文件是这样的:

<!-- Main theme -->
<style name="MainTheme" parent="MainTheme.Base">
</style>
<style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
  <item name="windowNoTitle">true</item>
  <item name="windowActionBar">false</item>
  <item name="android:windowBackground">@color/WindowBackground</item>
  <item name="colorPrimary">@color/Primary</item>
  <item name="colorPrimaryDark">@color/PrimaryDark</item>
  <item name="colorAccent">@color/Accent</item>
  <item name="android:textColorPrimary">@color/PrimaryText</item>
  <item name="android:textColorSecondary">@color/SecondaryText</item>
</style>

values-v21/styles.xml 内部,我有以下内容:

<!-- Main theme -->
<style name="MainTheme" parent="MainTheme.Base">
  <item name="android:windowTranslucentStatus">false</item>
  <item name="android:windowDrawsSystemBarBackgrounds">true</item>
  <item name="android:statusBarColor">@color/PrimaryDark</item>
</style>

但是,状态栏不会更新。但是,如果我从OnCreate() 使用它,颜色会更新得很好:

protected virtual void SetupStatusBar()
{
    if (Build.VERSION.SdkInt < BuildVersionCodes.Lollipop)
        return;

    Window.ClearFlags(WindowManagerFlags.TranslucentStatus);

    Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);

#pragma warning disable 618
    Window.SetStatusBarColor(Resources.GetColor(Resource.Color.PrimaryDark));
#pragma warning restore 618
}

我有点困惑,因为我所做的只是复制 XML 指令。

我使用的是运行 Android 5.1.1(API 22)的 Galaxy Tab S2,我认为应该触发 v21 样式覆盖。

【问题讨论】:

    标签: android xamarin android-appcompat


    【解决方案1】:

    我找到了。您对在 API 22 上覆盖主题的期望不会以这种方式实现。

    我假设在您的清单中您已将应用主题声明为 MainTheme

    在你的价值观-v21.所以你的代码会是这样的

    <!-- Main theme -->
    <style name="MainTheme>" parent="Theme.AppCompat.Light.DarkActionBar">
      <item name="android:windowTranslucentStatus">false</item>
      <item name="android:windowDrawsSystemBarBackgrounds">true</item>
      <item name="android:statusBarColor">@color/PrimaryDark</item>
    </style>
    

    或者以你的方式:

    <style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
      <item name="android:windowTranslucentStatus">false</item>
      <item name="android:windowDrawsSystemBarBackgrounds">true</item>
      <item name="android:statusBarColor">@color/PrimaryDark</item>
    </style>
    <style name="MainTheme" parent="MainTheme.Base">
    </style>
    

    所以现在 android 将引用 MainTheme 中的样式并替换重复的属性(如果有),优先考虑 values-21 xml。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-31
      • 1970-01-01
      • 2014-12-17
      • 2022-11-28
      • 2015-05-09
      • 2023-03-13
      相关资源
      最近更新 更多