[英]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.