如何删除CheckBox右侧不需要的空格?

mob*_*ner 13 checkbox android space center padding

我正在开发自定义列表视图.我想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>
Run Code Online (Sandbox Code Playgroud)

结果如下:http:
//pwong.name/checkbox_problem2.jpg

如您所见,复选框右侧有一些空格.我想要的是将复选框放在蓝色区域的中间.

是否可以删除不需要的空间?谢谢

小智 24

默认情况下,复选框具有 minWidth 和 minHeight 值

在此处输入图片说明

您可以将其值设置为 0

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minWidth="0dp"
    android:minHeight="0dp" />
Run Code Online (Sandbox Code Playgroud)

结果将是这样的,没有任何额外的空格

在此处输入图片说明

  • 不适用于 Android Studio 4.1。 (2认同)

ina*_*ruk 17

您可以CheckBox在LinearLayout 中换行,然后android:gravity="center"在该布局上使用.

 <LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="0dip" 
    android:layout_weight=".20"              
    android:background="#ff0000" 
    android:gravity="center">

    <CheckBox android:id="@+id/bus_route_list_item_reminder" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"                     
    />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

作为另一种选择,您可以使用RelativeLayout.这将大大简化您的布局,您将能够摆脱layout_weight.


Pav*_*lus 8

以前的解决方案都不适用于我,但我尝试对内容应用翻译,它运行得很好,不需要额外的布局层次结构,也不需要实现自己的视图:

                <CheckBox
                ...
                android:text="@null"
                android:translationX="12dp" />
Run Code Online (Sandbox Code Playgroud)

此外,它将元素的边界保持在适当的位置,因此触摸区域不会移位.


Mah*_*zad 7

translationX似乎工作。但是如果你想支持 RTL 布局,它会导致问题。另一种解决方案是将复选框的宽度设置为固定长度(例如26dp):

<CheckBox
    android:layout_width="26dp"
    android:layout_height="wrap_content"
    android:text="@null" />
Run Code Online (Sandbox Code Playgroud)