问题
I'm struggling to get Android to update my status bar color. I'm using AppCompatActivity
in Xamarin.Android.
My values/styles.xml
file is like so:
<!-- 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>
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>
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
}
I'm a bit confused, because all I'm doing is copying the XML directives.
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.
回答1:
I found it. Your expectation of overriding the theme on your API 22 will not be fulfilled this way.
I am assuming that in your manifest you have declared app theme as
MainTheme
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>
Or in your way:
<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>
So now android will reference styles from MainTheme and would replace the duplicate attributes if any, giving priority to values-21 xml.
来源:https://stackoverflow.com/questions/34858993/android-theme-will-not-update-status-bar-color