如何在Android中创建这种类型的复选框

如何在Android中创建这种类型的复选框,android,xml,checkbox,Android,Xml,Checkbox,如何在android中按下时创建这种类型的复选框。我试过了。我可以知道实现我的目标的正确方法吗?也许这个问题太基本了,但我没有找到任何合适的解决方案。请帮我解决 我想这样创作 未选中时,它将显示为: 这是我的复选框的XML代码: <CheckBox android:id="@+id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_conten

如何在android中按下时创建这种类型的
复选框
。我试过了。我可以知道实现我的目标的正确方法吗?也许这个问题太基本了,但我没有找到任何合适的解决方案。请帮我解决

我想这样创作

未选中时,它将显示为:

这是我的
复选框的XML代码:

 <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:button="@drawable/custom_checkbox"/>

下面是custom_checkbox.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:drawable="@drawable/unchecked" />
</selector>

这里是checked.xml

 <?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#54d66A" android:endColor="#54d66A" android:angle="270"/>
    <stroke android:width="4px" android:color="#ffc0c0c0" />
    <size android:height="20dp" android:width="20dp"/>
</shape>

以下是我的输出:

您需要创建一个带有
图层列表的
可绘制的
,其中两项为矩形。一个用于
背景
,另一个作为
笔划
,带有一些
边距

   <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
   <item>
    <shape android:shape="rectangle">
        <gradient android:startColor="#54d66A" android:endColor="#54d66A" android:angle="270"/>
        <size android:height="20dp" android:width="20dp"/>
    </shape>
  </item>
  <item android:bottom="2dp" android:left="2dp" android:right="2dp" android:top="2dp">
    <shape android:shape="rectangle" >
        <stroke android:width="2dp" android:color="#ffc0c0c0" />
    </shape>
  </item>
 </layer-list>


然后,将此可绘制设置为
state\u checked
background.

为什么不使用图像?出于我的学习目的,先生。使用另一个可绘制作为背景。问题是您不确定如何获取外部绿色环?