如何将自定义图像应用于android中的复选框

sal*_*roy 49 android

我正在尝试将自定义图像应用到android中的复选框,为此我创建了一个check_custom.xml文件,我在其中为不同状态的复选框定义自定义图像,如:

<item android:state_checked="true" 
      android:drawable="@drawable/btn_check_on" /> <!-- checked --> 
<item android:state_checked="false" 
      android:drawable="@drawable/btn_check_off" /> <!-- unchecked --> 
<item android:state_focused="true"
      android:drawable="@drawable/btn_check_onfocus" />    <!--on focus--> 
<item android:drawable="@drawable/btn_check_off" /> <!-- default --> 
Run Code Online (Sandbox Code Playgroud)

三个状态上的三个不同的图像在选中,焦点和未选中,然后我将此xml文件分配给复选框的背景属性,但我没有得到所需的结果,这种技术应用自定义图像以及默认图像两者.

DJC*_*DJC 97

萨尔曼,android:button而不是android:background.另见Custom复选框图片android

  • 欢迎.如果这工作,我会感谢点击左侧的复选框,这有助于回答者的代表:) (2认同)
  • 这很酷而且简单的家伙,不要把它标记为答案.我是+1 (2认同)

har*_*iss 18

当背景布局很暗时,我不喜欢复选框默认图像的外观,所以我使用xml选择器更改了它,以便在按钮中有白色背景.

android:button ="@ drawable/selector_check"中需要选择器

                 <CheckBox
                            android:id="@+id/checkBox1"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:gravity="left"
                            android:text="Show password"
                            android:textColor="@color/white"
                            android:button="@drawable/selector_check"
                            >
Run Code Online (Sandbox Code Playgroud)

以下是"drawable/selector_check.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="@android:drawable/checkbox_on_background" /> <!-- checked --> 
          <item android:state_checked="false" 
             android:drawable="@android:drawable/checkbox_off_background" /> <!-- unchecked-->
          </selector>
Run Code Online (Sandbox Code Playgroud)

这就是结果:

在此输入图像描述


WOU*_*nes 6

将btn_check.xml从android-sdk/platforms/android - #/ data/res/drawable复制到项目的drawable文件夹,并将"on"和"off"图像状态更改为自定义图像.

然后你的xml将需要 android:button="@drawable/btn_check"

<CheckBox
    android:button="@drawable/btn_check"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true" />
Run Code Online (Sandbox Code Playgroud)

如果你想使用不同的默认android图标,你可以使用android:button="@android:drawable/..."


小智 5

<item android:drawable="@drawable/ON" android:state_checked="false"/>
<item android:drawable="@drawable/OFF" android:state_checked="true"/>
<item android:drawable="@drawable/ON"/>
Run Code Online (Sandbox Code Playgroud)