【问题标题】:How Do Create This Type Of CheckBox In Android如何在 Android 中创建这种类型的 CheckBox
【发布时间】:2014-05-05 22:59:18
【问题描述】:

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

我想创建这样的

当它未选中时,它看起来像:

这是我的checkbox 的 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>

这是我的输出:

【问题讨论】:

  • 为什么不用图片?
  • 为了我的学习目的先生。
  • 使用另一个drawable作为背景。
  • 是你不知道外绿环怎么弄的问题?

标签: android xml checkbox


【解决方案1】:

您需要创建一个drawablelayer-list,其中两个项目为矩形。一个是background,另一个是stroke,还有一些margin

   <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>

并且,将此drawable设置为state_checked背景。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多