以编程方式实现android:button ="@ drawable/checkbox"

use*_*511 6 checkbox android android-widget android-button

我正在尝试创建自定义复选框按钮图像.经过一些研究,我发现了这个代码示例:

<CheckBox android:id="@+id/chkFav" android:layout_width="wrap_content"
        android:layout_marginRight="0dp" android:button="@drawable/checkbox"
              android:layout_height="wrap_content" android:clickable="true"/>
Run Code Online (Sandbox Code Playgroud)

我的查询是如何在代码中实际实现android:按钮.

PH7*_*PH7 11

setButtonDrawable(Drawable d)可行的方法.确保它是状态列表可绘制以响应用户交互.

一步一步的指示

  1. 至少有2张图片.(一个用于检查状态,另一个用于正常状态)
  2. 创建xml drawable.http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
  3. 使用setButtonDrawable(R.drawable.your_xml_drawable).

**笔记 - 有很多方法可以实现.这只是一种简单的方法.

  • setButtonDrawable(getResources().getDrawable(android.R.color.transparent))应该做的伎俩. (7认同)