xamarin.forms 中开关为 "on"(蓝色到绿色)时如何更改默认颜色在 ios 中默认为绿色,但在 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
当它 "on" 时,我得到的代码是蓝色的。我想把它改成绿色。
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,
},
}
};
}
}
}
这对我有用 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;
}
"default"状态需要添加到最后。
希望对您有所帮助。
对于您在 Xamarin 上的 IOS 项目,您只需将此行放入 AppDelegate.cs
的 FinishedLaunching 方法
UISwitch.Appearance.OnTintColor = Color.FromHex("#yourColor").ToUIColor();
当它 "on" 时,我得到的代码是蓝色的。我想把它改成绿色。
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,
},
}
};
}
}
}
这对我有用 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;
}
"default"状态需要添加到最后。
希望对您有所帮助。
对于您在 Xamarin 上的 IOS 项目,您只需将此行放入 AppDelegate.cs
的 FinishedLaunching 方法 UISwitch.Appearance.OnTintColor = Color.FromHex("#yourColor").ToUIColor();