当它“打开”时,我得到的代码是蓝色的。我想把它改成绿色。
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;
}
“默认”状态需要作为最后添加。
希望对您有所帮助。
关于c# - xamarin.forms 中开关为 "on"(蓝色到绿色)时如何更改默认颜色在 ios 中默认为绿色,但在 android 中为蓝色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28672625/