简体   繁体   English

如何在表单xamarin中将开关设置为“on”(蓝色到绿色)时更改默认颜色。 在ios默认它是GReen但在android中它是蓝色的

[英]how to change default color when switch is “on”(blue to green) in forms xamarin. In ios default it is GReen but in android it is blue

wirth this code i am getting swith in blue color when it "on". wirth这个代码当它“开启”时,我正在用蓝色开关。 i want to change it to green. 我想把它变成绿色。

using Xamarin.Forms;

namespace swithcasedemo
{
    public class MyPage : ContentPage
    {
        public MyPage ()
        {
            Content = new StackLayout { 
                Children = {
                    new Label { Text = "Hello ContentPage",
                        HorizontalOptions=LayoutOptions.Center,
                        VerticalOptions=LayoutOptions.CenterAndExpand
                    },
                    new Switch{
                        HorizontalOptions=LayoutOptions.Center,
                        VerticalOptions=LayoutOptions.CenterAndExpand,


                    },


                }
            };
        }
    }
}

This worked for me using Android >= 4.1 这适用于我使用Android> = 4.1

//need to reference Drawables otherwise StateListDrawable is not recognized.
using Android.Graphics.Drawables;

//...
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
       //...
       mySwitch = view.FindViewById<Switch>(Resource.Id.theSwitchIdFromXml);
       //... 

        Android.Graphics.Color colorOn = Android.Graphics.Color.Green;
        Android.Graphics.Color colorOff = Android.Graphics.Color.Brown;
        Android.Graphics.Color colorDisabled = Android.Graphics.Color.Gray;

        StateListDrawable drawable = new StateListDrawable();
        drawable.AddState(new int[] { Android.Resource.Attribute.StateChecked }, new ColorDrawable(colorOn));
        drawable.AddState(new int[] { -Android.Resource.Attribute.StateEnabled }, new ColorDrawable(colorDisabled));
        drawable.AddState(new int[] { }, new ColorDrawable(colorOff));

        mySwitch.ThumbDrawable = drawable;
  }

The "default" state needs to be added as the last. 需要添加“默认”状态作为最后一个。

I Hope it will help. 我希望它会有所帮助。

对于您在Xamarin上的IOS项目,您只需要将此行放到AppDelegate.cs的FinishedLaunching方法中

            UISwitch.Appearance.OnTintColor = Color.FromHex("#yourColor").ToUIColor();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 红色,绿色和蓝色值阵列为位图着色 - Red, Green and Blue value arrays to color bitmap 如何从像素更改红色/绿色/蓝色/ alpha值的值 - How to change value of red/green/blue/alpha values from pixels 如何将整数转换为红色,绿色和蓝色值 - How to convert an Integer to Red, Green, and Blue values 如何更改RichTextBox默认的蓝色突出显示颜色? - how to can Change the richtextbox default blue highlight color? 在Xamarin.iOS中从{alpha,red,green,blue}值创建CGColor - Create CGColor from { alpha, red, green, blue } values in Xamarin.iOS IOS工具栏xamarin.forms上的图标颜色保持蓝色 - Icon color remains blue on IOS toolbar xamarin.forms 如何在asp:dropdownlist中更改用于选择的默认蓝色和用于文本的默认白色? - How to change the default blue color for selection and default white color for text in asp:dropdownlist? 如何使用Visual Studio扩展来创建绿色(或蓝色)的花体装饰 - How to create green (or blue) squiggle adornments with a Visual Studio extension 将telerik:RadGridView的默认颜色从黑色更改为蓝色 - change default color of telerik:RadGridView from black to to blue 如何在将鼠标悬停在按钮上时摆脱默认的蓝色? - How to get rid of the default blue color on hover over a button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM