我无法在Xamarin.Forms中使用样式设置Android开关的颜色

时间:2018-07-07 12:12:14

标签: xamarin xamarin.forms

我正在使用以下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>

2 个答案:

答案 0 :(得分: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>

enter image description here

自定义渲染器将允许对样式进行更多控制。以下自定义渲染器可用于引用您的自定义样式(请注意,您需要将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>

enter image description here

答案 1 :(得分:0)

在Visual Studio中,检查您的android项目并找到以下文件:

Resources -> values -> styles.xaml

您可以在此项目中更改所需的颜色:

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

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

您无法在here

中找到有关colorAccent的更多信息