我正在尝试创建自定义复选框按钮图像。经过一些研究,我遇到了这个代码示例:
<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"/>
我的问题是如何在代码中实际实现 android:button。
最佳答案
setButtonDrawable(Drawable d)是要走的路。确保它是可绘制状态列表以响应用户交互。
一步一步的指导
- 至少有 2 张图片。 (一个用于检查状态,另一个用于正常状态)
- 创建 xml 可绘制对象。 http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
- 使用 setButtonDrawable(R.drawable.your_xml_drawable)。
**Notes--实现的方法有很多种。这只是一种简单的方法。
关于android - 实现android :button ="@drawable/checkbox" programmatically,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7342841/