简体   繁体   中英

Can't change Theme color on Status bar android/xamarin

I've checked all solutions for this, but non was working in my case. I can't get why the color of status bar won't change(it is gray by default) In styles I define:

 <style name="Base.Theme.DesignDemo" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">#2e662f</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">#4CAEE3</item>
  </style>  

In AndroidManifest:

<application android:label="App1" android:theme="@style/Theme.DesignDemo" android:icon="@drawable/icon">

And inside the Activity I'm calling like this:

[Activity(Label = "", MainLauncher = true, Icon = "@drawable/icon")]

I also tried something like this:

 [Activity(Label = "", Theme = "@style/Theme.DesignDemo",MainLauncher = true, Icon = "@drawable/icon")]

Make sure the android:targetSdkVersion is set in the AndroidManifest.xml file or alternatively you can use code if you prefer.

You can change the status bar color for Xamarin Android apps programmatically with the following code translated from Java :

if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
    // clear FLAG_TRANSLUCENT_STATUS flag:
    Window.ClearFlags(Android.Views.WindowManagerFlags.TranslucentStatus);

    //Window.ClearFlags(WindowManager.Pa WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    Window.AddFlags(Android.Views.WindowManagerFlags.DrawsSystemBarBackgrounds);

    // finally change the color
    Window.SetStatusBarColor(new Color(ContextCompat.GetColor(this, Resource.Color.colorPrimaryDark)));
}

Make sure your theme is using the Theme.AppCompat style and then your activity should inherit from AppCompatActivity . So you should have something like this :

[Activity(Label = "Dialogs Demo", MainLauncher = true, Icon = "@mipmap/icon", Theme = "@style/AppTheme")]
    public class MainActivity : AppCompatActivity
    {}

And a sample style like this :

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
  <!-- Customize your theme here. -->
  <item name="colorPrimary">@color/colorPrimary</item>
  <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  <item name="colorAccent">@color/colorAccent</item>
</style>

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