
如上图所示.此屏幕截图有三种视图.
- 第一项是CheckBox文本并关闭状态.
- 第二项CheckBox没有文字,状态开启.
- 最后一项是ImageViewsrc指向可绘制图像.
CheckBoxes是使用自定义的android:button.

当我尝试使用较小的图像时,所有复选框都是左对齐的.
比较这两个图像告诉我,默认大小CheckBox似乎固定为一定大小,直到文本属性足够大,需要扩展.
文件中没有任何特殊内容.见下文.
custom_cb.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/creditcard_selected" android:state_checked="true" />
<item android:drawable="@drawable/creditcard"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox android:id="@+id/cbFalse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/custom_cb"
android:text="" />
<CheckBox android:id="@+id/cbTrue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/custom_cb"
android:focusable="false"
android:checked="true"
android:layout_toRightOf="@id/cbFalse" />
<ImageView android:id="@+id/imvTrue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/creditcard"
android:layout_toRightOf="@id/cbTrue" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
无论如何,我可以使用更大的图像CheckBox同时保持大小wrap_content?如果我将CheckBox设置layout_width为实际像素或dp,则显示完整图像,但这意味着每次更改时都必须手动检查大小.
Luc*_*epe 59
今天我遇到了同样的问题(我的自定义图像在左侧被剪切).我把它修好了:
android:button="@null"
android:background="@drawable/my_custom_checkbox_state.xml"
Run Code Online (Sandbox Code Playgroud)
Sri*_*thi 15
只是用
android:button="@null"
android:background="@null"
android:drawableLeft="your custom selector"
android:drawablePadding="as you need"
android:text="your text"
Run Code Online (Sandbox Code Playgroud)
多数民众赞成......工作正常..
尝试使用水平方向的线性布局而不是相对布局。还可以在每个布局中使用权重来强制视图使用相同的宽度。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox android:id="@+id/cbFalse"
android:weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:button="@drawable/custom_cb"
android:text="" />
<CheckBox android:id="@+id/cbTrue"
android:weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:button="@drawable/custom_cb"
android:focusable="false"
android:checked="true"
android:layout_toRightOf="@id/cbFalse" />
<ImageView android:id="@+id/imvTrue"
android:weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/creditcard"
android:layout_toRightOf="@id/cbTrue" />
Run Code Online (Sandbox Code Playgroud)