【问题标题】:Xamarin forms: Customize Android Material style status bar colorXamarin 表单:自定义 Android Material 样式状态栏颜色
【发布时间】:2016-04-17 13:36:50
【问题描述】:

我使用以下代码在我的 Xamarin.Forms 项目上启用了 Material 主题,但在 Android(棒棒糖和棉花糖)上,顶部状态栏(时钟、信号、电池等所在的位置)没有改变.它始终保持黑色。 我已经阅读了很多论坛和博客,尝试了很多组合,但我无法让这个状态栏按我的意愿着色。

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>

感谢您的帮助!

【问题讨论】:

    标签: xamarin material-design xamarin.forms statusbar android-theme


    【解决方案1】:

    您应该关注this Tutorial 以获得成功。

    基本上你必须使用FormsAppCompatActivity 而不是FormsApplicationActivityTheme.AppCompat.Light.NoActionBar 而不是Theme.Material.Light

    【讨论】:

    • 谢谢!有没有可以看到所有可能设置的地方或用户指南?例如更改文本条目的条形颜色,或去除按钮的阴影等。
    • 这通常在Android开发指南中定义。 developer.android.com/training/material/theme.html 是一个很好的起点。但结合 AppCompat,它总是有点繁琐
    【解决方案2】:

    其他最简单的解决方案,在 MainActivity.cs 的 OnCreate 方法中添加:

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

    【讨论】:

    • 不幸的是,xamarin 说:“Java.Lang.NoSuchMethodError: 在 Landroid/view/Window 类中没有 name='setStatusBarColor' signature='(I)V' 的方法;” 上帝,我讨厌 xamarin,没有什么可以保证工作。他们为什么首先发布它?
    • 完美运行。谢谢。
    • Maxime Claude SetStatusBarColor 在 Android Lollipop 下不可用,必要时添加验证
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      我的答案与接受的答案相同。它正在处理我们的一个项目,但不是另一个。

      我还没有看到这个特定问题的解决方案,所以我是第一个。

      如果您在 assemblyInfo.cs 而不是清单中声明了您的使用权限,则会导致状态栏无法按预期工作。

      即使您通过为每个活动单独设置它来使其工作,当您转换到活动时它仍然无法工作。因此,唯一的方法是将使用权限语句移动到清单中。

      希望我能帮助别人!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-17
        • 1970-01-01
        • 2015-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多