Xamarin Forms 中 Android 上的圆形复选框

Jon*_*anK 1 xamarin xamarin.forms

有谁知道如何为 Xamarin 表单复选框制作自定义渲染器?我希望该复选框在 Android 设备上呈圆形,就像在 iOS 上一样。

我正在使用 Xamarin Forms 4.3。

Kru*_*dia 5

这是使用库的第一个解决方案:

您的解决方案就在这里。

因为您可以根据需要改变形状。

这是一个 Gihub 链接,如下所示

这是根据您的需要的第二个解决方案,这是椭圆形的复选框渲染器。

在 Xaml 中,

<CheckBox/>
Run Code Online (Sandbox Code Playgroud)

在Android项目中,首先创建新文件。我将其命名为CheckboxRenderer.cs

在命名空间之前设置:

[assembly: ExportRenderer(typeof(Xamarin.Forms.CheckBox), typeof(CheckboxRenderer))]
Run Code Online (Sandbox Code Playgroud)

这是我的班级文件,

public class CheckboxRenderer : ViewRenderer<Xamarin.Forms.CheckBox, Android.Widget.CheckBox>
{
    private Android.Widget.CheckBox checkBox;

    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.CheckBox> e)
    {
        base.OnElementChanged(e);
        var model = e.NewElement;
        checkBox = new Android.Widget.CheckBox(Context);
        checkBox.SetButtonDrawable(Resource.Drawable.custom_checkbox);
        checkBox.Tag = this;
        SetNativeControl(checkBox);
    }
}
Run Code Online (Sandbox Code Playgroud)

在 Drawble 文件夹中创建这 3 个xml文件,

1) 自定义复选框.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
  <item android:state_checked="true"
      android:drawable="@drawable/checked" />
  <item android:state_pressed="true"
      android:drawable="@drawable/checked" />
  <item android:state_pressed="false"
      android:drawable="@drawable/unchecked" />
</selector>
Run Code Online (Sandbox Code Playgroud)

2)检查.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_checked="true">
    <layer-list>
      <item>
        <shape android:shape="oval">
          <corners android:radius="1dp" />
          <stroke
              android:width="1dp"
              android:color="#777" />
          <gradient
              android:startColor="#990000"
              android:centerColor="#990000"
              android:endColor="#990000"
              android:angle="270" />
          <size
              android:width="30dp"
              android:height="30dp" />
        </shape>
      </item>

      <item
          android:width="8dp"
          android:height="2dp"
          android:top="20dp"
          android:left="6dp">
        <rotate
            android:fromDegrees="45">
          <shape android:shape="rectangle">
            <solid android:color="#fff"/>
          </shape>
        </rotate>
      </item>

      <item
          android:width="19dp"
          android:height="2dp"
          android:top="16dp"
          android:left="9dp">
        <rotate
            android:fromDegrees="-45">
          <shape android:shape="rectangle">
            <solid android:color="#fff"/>
          </shape>
        </rotate>
      </item>
    </layer-list>
  </item>

</selector>
Run Code Online (Sandbox Code Playgroud)

3) 未检查.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
  <corners android:radius="1dp" />
  <stroke
      android:width="1dp"
      android:color="#777" />
  <gradient
      android:startColor="#990000"
      android:centerColor="#990000"
      android:endColor="#990000"
      android:angle="270" />
  <size
      android:width="30dp"
      android:height="30dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)

哦,完成了......你会得到这样的输出,

未选中 已检查

希望这有帮助。