【问题标题】:How to remove padding around Android CheckBox如何删除 Android CheckBox 周围的填充
【发布时间】:2013-01-07 21:49:19
【问题描述】:

我需要将检查放在我的图像视图的右上角。但是当我这样做时,我注意到我的复选框周围有一个默认边距。有没有办法去掉这个??

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" >

        <ImageView
            android:id="@+id/thumbImage"
            android:layout_width="100dp"
            android:layout_height="132dp"
            android:layout_gravity="center" />
    </FrameLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="#ff0000"
        android:gravity="center" >

        <CheckBox
            android:id="@+id/itemCheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_gravity="center"
            android:button="@drawable/checkbox_background"
            android:paddingLeft="0dp"
            android:paddingTop="0dp" />

    </LinearLayout>

</RelativeLayout>

【问题讨论】:

    标签: android checkbox margin padding


    【解决方案1】:

    如果您查看在 onDraw() 方法中由 Checkbox 类扩展的 CompoundButton 类的 source code,则填充取决于重力

      @Override
        protected void onDraw(Canvas canvas) {
            ...
    
                final int top;
                switch (verticalGravity) {
                    case Gravity.BOTTOM:
                        top = getHeight() - drawableHeight;
                        break;
                    case Gravity.CENTER_VERTICAL:
                        top = (getHeight() - drawableHeight) / 2;
                        break;
                    default:
                        top = 0;
                }
           ...
        }
    

    所以你可以做的是将属性gravity的xml值设置为
    android:gravity="top|start"

    但这只能在您不想在复选框中使用文本时使用,因为重力用于使文本居中。

    【讨论】:

      【解决方案2】:

      在 android 中删除单选按钮默认边距或填充

      如果你使用 xml

      android:minWidth="0dp"
      android:minHeight="0dp"
      

      如果以编程方式使用

       radiobtn.setMinimumHeight(0);
       radiobtn.setMinimumWidth(0);
      

      【讨论】:

      • 谢谢!这个最简单,不涉及负边距!
      • 这是唯一适用于所有情况的方法,因为其他答案中提到的负边距不适用于ConstraintLayout
      【解决方案3】:

      这在约束布局中对我有用。我希望它适用于另一种布局。

      <androidx.appcompat.widget.AppCompatCheckBox
                      ...
                      android:translationX="-5dp"
                       />
      

      【讨论】:

      • 只有这个有帮助。谢谢大佬
      【解决方案4】:

      感谢@VladislavShcherbakov 的评论,它是:

      android:paddingLeft="-5dp"
      android:layout_marginStart="-5dp"
      android:layout_marginLeft="-5dp"
      // android:translationX="-5dp"
      

      【讨论】:

        【解决方案5】:

        现在有更好的方法:

        android:minWidth="0dp"
        android:minHeight="0dp"
        

        【讨论】:

        • @Seza 尝试使用来自com.google.android.material:material:1.0.0的 MaterialCheckBox
        • 感谢您的提示。我真的希望 AndroidX 能解决这个问题
        • 我认为答案很简单也很好。它在没有文本的情况下删除了复选框中的右填充。
        • 对于以编程方式执行此操作并遇到困难的任何人,请尝试使用 minimumHeightminimumWidth 而不是 minWidthminHeight
        • @masterwok 谢谢,正在找这个。
        【解决方案6】:

        为什么要使用负填充等等来复杂化。使用简单直接的方法,为 FrameLayout 提供填充,而不是在最上面的相对布局中进行填充。由于您在第二个布局中只有 CheckBox,因此无需为其提供填充。

        如果您遇到任何问题或有任何疑问,请告诉我

        【讨论】:

        • 只是说因为在问题中您需要的不是填充,而是已经有一些填充需要删除。这只发生在较旧的 android 版本中,其中刻度的尾巴超出了复选框
        【解决方案7】:

        您可以使用负边距。

        android:layout_marginTop = "-5dp"
        android:layout_marginRight = "-5dp"
        

        【讨论】:

        • 请注意,您的测试可能会失败,因为 espresso“不会看到至少 90%”的复选框。
        • ConstraintLayout 有什么解决方案吗?
        • 你可以使用 android android:translationX="-5dp",但这是不好的解决方案
        • 最佳答案。谢谢。
        猜你喜欢
        • 2013-07-31
        • 2012-04-09
        • 2017-08-28
        • 2018-03-22
        • 2014-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-22
        相关资源
        最近更新 更多