简体   繁体   中英

Can't change color of status bar

I am trying to theme my app to follow the material Android look. My project is a Xamarin.Android project, which targets API 23, but the minimum API target is 16. So I have to use the AppCompat library to have the same design on all APIs.

I have followed these guides: Material Theme , Beautiful Material Design with the Android Support Design Library , Android Tips: Hello AppCompatActivity, Goodbye ActionBarActivity and created these files:

  • Resources/values/styles.xml:

     <resources> <style name="MyTheme" parent="MyTheme.Base"> </style> <style name="MyTheme.Base" parent="Theme.AppCompat.Light"> <item name="colorPrimary">#F44336</item> <item name="colorPrimaryDark">#D32F2F</item> <item name="colorAccent">#FF4081</item> </style> </resources>
  • Resources/values-v21/styles.xml

     <resources> <!-- Base application theme for API 21+. This theme replaces MyTheme from resources/values/styles.xml on API 21+ devices. --> <style name="MyTheme" parent="MyTheme.Base"> <item name="android:windowContentTransitions">true</item> <item name="android:windowAllowEnterTransitionOverlap">true</item> <item name="android:windowAllowReturnTransitionOverlap">true</item> <item name="android:windowSharedElementEnterTransition">@android:transition/move</item> <item name="android:windowSharedElementExitTransition">@android:transition/move</item> </style> </resources>
  • MainActivity.cs

     using Android.App; using Android.Widget; using Android.OS; using Android.Support.V7.App; namespace TestThemeApp { [Activity(Label = "TestThemeApp", MainLauncher = true, Icon = "@drawable/icon", Theme = "@style/MyTheme")] public class MainActivity : AppCompatActivity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView (Resource.Layout.Main); } } }

I'm just trying to make the the toolbar red, and the status bar a darker red, but the status bar color won't change. This is what it looks like:

状态栏不会改变

What did I do wrong?

Try this in your style (Resources/values-v21/styles.xml)

 <item name="android:windowDrawsSystemBarBackgrounds">true</item> > <item name="android:statusBarColor">@color/cobalt_light</item>

I personaly prefer to handle it in the onCreate method of the activity:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.setStatusBarColor(getResources().getColor(R.color.gray_very_dark));
    window.setNavigationBarColor(getResources().getColor(R.color.gray_darker));
}

For a simple workaround you can change your primary color to the desired color of your status bar and it will change the color of your status bar automatically.

NOTE: It is a work around till you find the correct answer.

You'll have to add a flag at the top of the MainActivity and set the bar color at the end.

public class MainActivity : AppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        // First check if the current SDK is >= 21. Else it will cause trouble
        if ((int)Build.VERSION.SdkInt >= 21)
            Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);

        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        SetBarColor();
    }

    public void SetBarColor ()
    {
        if((int)Build.VERSION.SdkInt >= 21)
            Window.SetStatusBarColor(Color.ParseColor("#FF0000"));
    }
}

Use below code snippet to change statusbar color:

public void setStatusBarColor(Activity context, int color) {
            if (Build.VERSION.SDK_INT >= 21) {
                Window window = context.getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.setStatusBarColor(ContextCompat.getColor(context,color));
            }
        }

I think that i have found the most elegant solution based on the other answers:

The only thing i changed was added the flag in the OnCreate(Bundle bundle) function:

 protected override void OnCreate(Bundle bundle)
 {
       if ((int)Build.VERSION.SdkInt >= (int)BuildVersionCodes.Lollipop)
                Window.AddFlags(Android.Views.WindowManagerFlags.DrawsSystemBarBackgrounds);

       base.OnCreate(bundle);

       // Set our view from the "main" layout resource
       SetContentView (Resource.Layout.Main);
 }  

And that's it! The style files do the rest, and on older version of android, the status bar just won't change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM