Android主题将不会更新状态栏颜色

Android主题将不会更新状态栏颜色

问题描述:

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

I'm struggling to get Android to update my status bar color. I'm using AppCompatActivity in Xamarin.Android.

我的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里面,我有以下内容:

Inside of values-v21/styles.xml, I have the following:

<!-- 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()开始,颜色会更新得很好:

However, the status bar will not update. If I use this however, from OnCreate(), the color updates just fine:

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指令.

I'm a bit confused, because all I'm doing is copying the XML directives.

我认为我正在使用运行Android 5.1.1(即API 22)的Galaxy Tab S2,并且应该触发v21样式覆盖.

I'm using a Galaxy Tab S2 running Android 5.1.1, which is API 22, and should trigger the v21 style override, I'd think.

我找到了它.您无法通过这种方式满足您对API 22覆盖主题的期望.

I found it. Your expectation of overriding the theme on your API 22 will not be fulfilled this way.

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

I am assuming that in your manifest you have declared app theme as MainTheme

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

in your values-v21. So your code would be like this

<!-- 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.

So now android will reference styles from MainTheme and would replace the duplicate attributes if any, giving priority to values-21 xml.