简体   繁体   English

如何删除 Android CheckBox 周围的填充

[英]How to remove padding around Android CheckBox

I need to put check to the right hand top corner of my imageview.我需要把支票放在我的图像视图的右上角。 But when I do this I noticed a default margin around my checkbox.但是当我这样做时,我注意到我的复选框周围有一个默认的边距。 Is there a way to remove this??有没有办法去掉这个??

<?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>

There's now a better way of doing this:现在有一个更好的方法来做到这一点:

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

You could use a negative margin.您可以使用负边距。

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

To remove radio button default margins or padding in android在android中删除单选按钮默认边距或填充

if you using xml如果你使用 xml

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

if using programmatically如果以编程方式使用

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

This works for me in Constraint Layout .这在Constraint Layout 中对我有用 I hope it will work for another layout.我希望它适用于另一种布局。

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

If you look at the source code for the CompoundButton class that is extended by the Checkbox class at the onDraw() method, the padding depends from the gravity如果查看由Checkbox类在onDraw()方法中扩展的 CompoundButton 类的源代码,则填充取决于重力

  @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;
            }
       ...
    }

So what you can do is set the xml value for the attribute gravity to所以你可以做的是将属性gravity的xml值设置为
android:gravity="top|start"

But this can be used only if you don't want to use text with your checkbox, since the gravity is used to center the text.但只有当您不想在复选框中使用文本时才可以使用此方法,因为重力用于使文本居中。

Thanks to @VladislavShcherbakov comment, it is:感谢@VladislavShcherbakov 的评论,它是:

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

Why to complicate with a negative padding and all.为什么要复杂化负填充等等。 Use the simple and straight way, give padding to the FrameLayout instead tof the top most Relative layout that would do the trick.使用简单直接的方法,给 FrameLayout 提供填充,而不是最上面的相对布局,这样就可以了。 As you have only the CheckBox in the second layout there is no need to give padding for it.由于您在第二个布局中只有 CheckBox,因此无需为其提供填充。

Please do let me know if you face any problems or have any further doubts如果您遇到任何问题或有任何进一步的疑问,请告诉我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM