Xamarin表单:自定义Android Material样式状态栏颜色

4
我已经在我的Xamarin.Forms项目中使用以下代码启用了Material主题,但是在Android(Lollipop和Marshmallow)上,顶部状态栏(其中包含时钟、信号、电池等)没有改变。它一直是黑色的。 我已经阅读了很多论坛和博客,并尝试了很多组合,但我无法使这个状态栏变成我想要的颜色。
MainActivity.cs
[Activity(Theme = "@style/MyTheme", Label = "MyApp", Icon = "@drawable/AppIcon", ScreenOrientation = ScreenOrientation.Portrait, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]

values-v21/style.xml

<resources>
  <style name="MyTheme.Splash" parent="android:Theme.Material.Light">    
    <item name="android:windowBackground">@drawable/SplashScreen</item>
    <item name="android:windowNoTitle">true</item>
  </style>
  <style name="MyTheme" parent="android:Theme.Material.Light">
    <item name="android:windowDrawsSystemBarBackgrounds">false</item>
    <item name="android:colorPrimary">@color/Brand</item>
    <item name="android:textColorPrimary">@color/White</item>
    <item name="android:statusBarColor">@color/BrandDark</item>
    <item name="android:colorPrimaryDark">@color/BrandDark</item>
    <item name="android:colorAccent">@color/Brand</item>
    <item name="android:textColor">@color/Brand</item>    
  </style>  
</resources>

values/colors.xml

<resources>  
  <color name="SplashBackground">#f78b2b</color>
  <color name="Brand">#f78b2b</color>
  <color name="BrandDark">#e47108</color>
  <color name="White">#ffffff</color>
</resources>

感谢您的帮助!

该句话与IT技术无关。
4个回答

4
您需要遵循此教程来获得成功。
基本上,您需要使用FormsAppCompatActivity而不是FormsApplicationActivity,并且使用Theme.AppCompat.Light.NoActionBar代替Theme.Material.Light

谢谢!有没有任何地方或用户指南,我可以查看所有可能的设置?例如更改文本输入框条的颜色,或删除按钮的阴影等。 - Stefano
这通常在Android开发指南中定义。http://developer.android.com/training/material/theme.html是一个很好的起点。但与AppCompat结合使用时,总是有些棘手。 - Sven-Michael Stübe

4

另一种解决方法非常简单,在MainActivity.cs的OnCreate方法中添加:

Window window = this.Window; window.ClearFlags(WindowManagerFlags.TranslucentStatus); window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds); window.SetStatusBarColor(Android.Graphics.Color.Rgb(116, 181, 13));

此代码将清除状态栏的透明标志,并设置系统栏背景颜色为RGB(116,181,13)。

不幸的是,Xamarin报告说:“Java.Lang.NoSuchMethodError:在类Landroid/view/Window中没有名称为'setStatusBarColor'签名为'(I)V'的方法;” 天啊,我讨厌Xamarin,没有什么是保证能够工作的。他们为什么要首先发布它呢? - Maxime Claude
完美运行。谢谢。 - Ahmed Elashker
Maxime Claude的SetStatusBarColor在Android Lollipop以下版本不可用,如有必要请添加验证。 - Benoit Canonne

1

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)));
}

0

我遇到了与被接受的答案相同的问题。它在我们的一个项目上运行正常,但在另一个项目上却不行。

我还没有看到这个特定问题的解决方案,所以我在这里第一个提出来。

如果你在assemblyInfo.cs中声明了uses-permissions而不是在清单中声明,会导致状态栏无法按预期工作。

即使你通过为每个活动单独设置它来使其工作,当你转换到其他活动时它仍然不起作用。所以唯一的方法是将uses-permission语句移到清单中。

希望我能帮助到某些人!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接