【问题标题】:How to remove unwanted spaces at the right of CheckBox?如何删除复选框右侧不需要的空格?
【发布时间】:2011-09-15 13:44:33
【问题描述】:

我正在处理自定义列表视图。我想在自定义视图中显示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>

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

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

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

【问题讨论】:

    标签: android android-layout checkbox material-design padding


    【解决方案1】:

    默认情况下,Checkbox 有 minWidth 和 minHeight 值

    您可以将其值设置为 0

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="0dp"
        android:minHeight="0dp" />
    

    结果会是这样,没有多余的空格

    【讨论】:

    • 您在应用中使用的 AppTheme 的父级是什么?
    • MaterialComponent,它是Android Studio 4.1以来的默认主题。
    • 对我来说效果很好。我的应用主题扩展自“Theme.MaterialComponents.NoActionBar”
    【解决方案2】:

    您可以将 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>
    

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

    【讨论】:

    • 这在 android 4.1.2 中不起作用。我认为这是一些错误。最好不要使用 CheckBox
    • 对于我不工作,我在复选框右侧获得空间,如何解决此问题
    • @rams 这个答案效果很好stackoverflow.com/a/64967815/8853657
    【解决方案3】:

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

                    <CheckBox
                    ...
                    android:text="@null"
                    android:translationX="12dp" />
    

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

    【讨论】:

    • 对我来说不工作,获取复选框右侧的空间
    • @rams 您可能启用了 RTL?在这种情况下,您可能需要使用 -12dp
    【解决方案4】:

    translationX 似乎有效。但是,如果您想支持 RTL 布局,就会出现问题。另一种解决方案是将复选框的宽度设置为固定长度(例如26dp):

    <CheckBox
        android:layout_width="26dp"
        android:layout_height="wrap_content"
        android:text="@null" />
    

    【讨论】:

      【解决方案5】:

      要删除图像右侧的多余空间(当没有文本时)扩展 CheckBox 类并覆盖 getSuggestedMinimumWidth() 方法以返回图像宽度。完整的解决方案:

      import android.content.Context;
      import android.graphics.drawable.Drawable;
      import android.os.Build;
      import android.util.AttributeSet;
      import android.widget.CheckBox;
      
      public class CheckBoxWithoutText extends CheckBox
      {
          private Drawable buttonDrawable;
      
          public CheckBoxWithoutText(Context context)
          {
              super(context);
          }
      
          public CheckBoxWithoutText(Context context, AttributeSet attrs)
          {
              super(context, attrs);
          }
      
          @Override
          protected int getSuggestedMinimumWidth()
          {
              if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
              {
                  return getCompoundPaddingLeft() + getCompoundPaddingRight();
              }
              else
              {
                  return buttonDrawable == null ? 0 : buttonDrawable.getIntrinsicWidth();
              }
          }
      
          @Override
          public void setButtonDrawable(Drawable d)
          {
              buttonDrawable = d;
              super.setButtonDrawable(d);
          }
      }
      

      【讨论】:

        【解决方案6】:

        我会在这里使用相对布局。对齐父右侧的复选框...

        问候, 斯蒂芬

        【讨论】:

          猜你喜欢
          • 2018-10-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-26
          • 2022-01-16
          • 2018-12-27
          • 1970-01-01
          相关资源
          最近更新 更多