フォームxamarinでスイッチが「オン」(青から緑)のときにデフォルトの色を変更する方法。iOSのデフォルトではGReenですが、Androidでは青色です

nnsr

このコードを使用すると、「オン」のときに青色になります。緑に変えたいです。

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,


                    },


                }
            };
        }
    }
}
ダニエレD。

これは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;
  }

最後に「デフォルト」状態を追加する必要があります。

お役に立てば幸いです。

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ