【问题标题】:I can't set the color of an Android switch in Xamarin.Forms with Styles我无法在 Xamarin.Forms 中使用样式设置 Android 开关的颜色
【发布时间】:2018-12-15 19:29:22
【问题描述】:

我正在使用以下 XML 来尝试更改 Xamarin 表单应用程序中开关的颜色。然而,什么都没有改变。
谁能给我任何关于我可能做错的建议:

<style name="MyTheme" parent="MyTheme.Base">
</style>
<!-- Base theme applied no matter what API -->
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">      
    <item name="windowActionBar">false</item>
    <item name="android:actionBarSize">45dp</item>
    <item name="colorPrimaryDark">#1976D2</item>
</style>

<style name="MyTheme.Switch" parent="Widget.AppCompat.CompoundButton.Switch">
    <item name="colorControlActivated">#FF0000</item>
    <item name="colorControlNormal">#FF0000</item>
    <item name="colorControlHighlight">#FF0000</item>
    <item name="colorSwitchThumbNormal">#FF0000</item>
    <item name="colorAccent">#FF0000</item>
 </style>

这是来自 MainActivity.cs 的剪辑

 [Activity(Label = "Japanese", Icon = "@drawable/Icon120", Theme = "@style/MyTheme", MainLauncher = true, 
          ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
          ScreenOrientation = ScreenOrientation.Portrait)]

由于这不起作用,我尝试了一些更改,但同样的问题是它不起作用:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CustomTheme" parent="@android:style/Theme">
        <item name="segmentedControlOptionStyle">@style/SegmentedControlOption</item>
        <item name="switchStyle">@style/SwitchCompat</item>
    </style>
</resources>

<style name="SwitchCompat" parent="@style/Widget.AppCompat.CompoundButton.Switch">
    <item name="colorPrimary">#FFFF00</item>
    <item name="colorPrimaryDark">#00FFFF</item>
    <item name="colorAccent">#FF00FF</item>
</style>

【问题讨论】:

标签: xamarin xamarin.forms


【解决方案1】:

MyTheme.Switch 样式不会自动应用于所有开关。如果colorAccent 移动到MyTheme.Base 内部,那么它可以改变色调颜色(注意colorAccent 也适用于其他小部件):

<style name="MyTheme" parent="MyTheme.Base">
</style>
<!-- Base theme applied no matter what API -->
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">      
    <item name="windowActionBar">false</item>
    <item name="android:actionBarSize">45dp</item>
    <item name="colorPrimaryDark">#1976D2</item>
    <!-- colorAccent is used as the default value for colorControlActivated which is used to tint widgets -->
    <item name="colorAccent">#FF0000</item>
</style>

自定义渲染器将允许对样式进行更多控制。以下自定义渲染器可用于引用您的自定义样式(请注意,您需要将 PanDemo.Droid 替换为您的命名空间)。

[assembly: Xamarin.Forms.ExportRenderer(
    typeof(Xamarin.Forms.Switch), 
    typeof(PanDemo.Droid.CustomSwitchRenderer))]

namespace PanDemo.Droid
{
    public class CustomSwitchRenderer : Xamarin.Forms.Platform.Android.SwitchRenderer
    {
        public CustomSwitchRenderer(Android.Content.Context context)
            : base(context) { }

        protected override Android.Widget.Switch CreateNativeControl()
        {
            return new Android.Widget.Switch(
                new Android.Views.ContextThemeWrapper(
                    this.Context, 
                    Resource.Style.MyTheme_Switch /* <- Custom Switch Style */));
        }
    }
}

然后根据需要修改您的自定义样式,例如:

<style name="MyTheme.Switch" parent="Widget.AppCompat.CompoundButton.Switch">
    <item name="colorAccent">#008000</item>
</style>

【讨论】:

  • 谢谢,有没有办法只将红色应用于开关?我之前看过自定义渲染器,但只需要开关颜色,但尝试这样做时遇到了问题。
  • @Alan2,我添加了一个可能满足您需求的示例自定义渲染器
【解决方案2】:

在visual studio中检查你的android项目并找到这个文件:

Resources -&gt; values -&gt; styles.xaml

您可以在此更改想要的颜色:

<item name="colorAccent">#3c94c7</item>

这将从您的 Android 主题中更改“colorAccent”。

您无法在 here 中找到有关 colorAccent 的更多信息

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-11
    • 1970-01-01
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多