如何删除CheckBox右侧不需要的空格?
我正在研究自定义列表视图。我想在自定义视图中显示一个 CheckBox
。 CheckBox
没有文本。我发现 CheckBox
右侧总是有一些空格。
这是我的布局 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal" android:background="#fa9654"
android:paddingTop="65dp" android:paddingBottom="65dp">
<TextView android:id="@+id/bus_route_list_item_num"
android:layout_height="wrap_content" android:layout_width="0dip"
android:gravity="center" android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="0.15"></TextView>
<TextView android:id="@+id/bus_route_list_item_station"
android:layout_height="wrap_content" android:layout_width="0dip"
android:gravity="left" android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight=".5"></TextView>
<TextView android:id="@+id/bus_route_list_item_fee"
android:layout_height="wrap_content" android:layout_width="0dip"
android:gravity="center" android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight=".15"></TextView>
<CheckBox android:id="@+id/bus_route_list_item_reminder" android:layout_height="wrap_content"
android:layout_width="0dip" android:layout_weight=".20" android:gravity="center"
android:layout_gravity="center" android:paddingRight="0dp" android:paddingLeft="0dp"
android:paddingTop="0dp" android:paddingBottom="0dp" android:background="#0066ff"
android:text=""
/>
</LinearLayout>
结果如下所示:
如您所见,右侧有一些空格复选框。我想要的是将复选框放在蓝色区域的中间。
是否可以删除不需要的空间?谢谢
I am working on a custom list view. I want to show a CheckBox
at the custom view. There is no text for the CheckBox
. I found it always have some spaces at the right of the CheckBox
.
Here is my layout xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal" android:background="#fa9654"
android:paddingTop="65dp" android:paddingBottom="65dp">
<TextView android:id="@+id/bus_route_list_item_num"
android:layout_height="wrap_content" android:layout_width="0dip"
android:gravity="center" android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="0.15"></TextView>
<TextView android:id="@+id/bus_route_list_item_station"
android:layout_height="wrap_content" android:layout_width="0dip"
android:gravity="left" android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight=".5"></TextView>
<TextView android:id="@+id/bus_route_list_item_fee"
android:layout_height="wrap_content" android:layout_width="0dip"
android:gravity="center" android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight=".15"></TextView>
<CheckBox android:id="@+id/bus_route_list_item_reminder" android:layout_height="wrap_content"
android:layout_width="0dip" android:layout_weight=".20" android:gravity="center"
android:layout_gravity="center" android:paddingRight="0dp" android:paddingLeft="0dp"
android:paddingTop="0dp" android:paddingBottom="0dp" android:background="#0066ff"
android:text=""
/>
</LinearLayout>
The result looks like:
As you can see there are some space at the right of the checkbox. What I want is put the checkbox at the middle of the blue area.
Is it possible to remove the unwanted space? thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
默认情况下,复选框具有 minWidth 和 minHeight 值
您可以将其值设置为 0
结果将是这样的,没有任何额外的空格
by default, the Checkbox has minWidth and minHeight value
you can set its value to 0
The result will be like that without any extra spaces
您可以将
CheckBox
包装在 LinearLayout 中,然后在该布局上使用android:gravity="center"
。作为另一种选择,您可以使用
RelativeLayout
。这将极大地简化您的布局,并且您将能够摆脱layout_weight
。You can wrap
CheckBox
in LinearLayout and then useandroid:gravity="center"
on that layout.As another alternative, you can use
RelativeLayout
. This would greatly simplify you layout and you will be able to get rid oflayout_weight
.以前的解决方案都不适合我,但我尝试对内容应用翻译,并且效果很好,不需要额外的布局层次结构,也不需要实现自己的视图:
此外,它将元素的边界保留在适当的位置,因此触摸区域没有移动。
Neither of previous solutions worked for me, but I've tried applying a translation to the content and it worked pretty well, no need in additional layout hierarchy, nor implementing own views:
Also, it keeps bounds of the element in proper place, so touch area is not shifted.
translationX
似乎有效。但如果你想支持 RTL 布局,就会出现问题。另一种解决方案是将复选框的宽度设置为固定长度(例如26dp
):The
translationX
seems to work. But it causes problem if you want to support RTL layouts. Another solution would be to set the width of checkbox to a fixed length (e.g.26dp
):要删除图像右侧的额外空间(当没有文本时),请扩展 CheckBox 类并重写 getSuggestedMinimumWidth() 方法以返回图像宽度。完整的解决方案:
To remove extra space at right of the image (when there is no text) extend CheckBox class and override getSuggestedMinimumWidth() method in order to return there image width. Complete solution:
我会在这里使用相对布局。正在父级右侧对齐复选框...
问候,
史蒂芬
I would use a relative layout here. Aligning checkbox on parent right...
Regards,
Stéphane