简体   繁体   English

如何删除Android复选框的内部图像填充(顶部,左侧和底部)

[英]How to remove internal Image padding (Top, Left and Bottom) of an Android Checkbox

I have a Checkbox in Android with a TextView above it: 我在Android上有一个Checkbox,上面有一个TextView:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="@dimen/default_margin">

    <TextView
        android:id="@+id/tv_edit_checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="false"
        android:layout_margin="0dp"
        android:gravity="center_vertical|center_horizontal"
        android:text="@string/edit_checkbox" />

    <CheckBox
        android:id="@+id/cb_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:checked="true" />

</LinearLayout>

The problem with Checkbox however is that it's internal "Image" got an invisible border around it. 然而,Checkbox的问题在于它的内部“图像”周围有一个看不见的边框。 How to remove this? 如何删除? I've tried android:padding="-5dp" but this only worked for the right side of the CheckBox for some reason, not the top, left and bottom (my main concern is the Top). 我已经尝试过android:padding="-5dp"但这只适用于CheckBox的右侧,出于某种原因,而不是顶部,左侧和底部(我的主要关注点是Top)。

Here is a screenshot of the Checkbox in Eclipse Graphical Layout window: 以下是Eclipse Graphical Layout窗口中Checkbox的屏幕截图:

LineairLayout selected: 选择LineairLayout: 在此输入图像描述 Checkbox selected: 选中复选框: 在此输入图像描述

In the second picture you can see the Checkbox has some padding around it when you select it. 在第二张图片中,您可以看到Checkbox在您选择它时会在其周围添加一些填充。 I want to have this removed (especially on the top), so that the space between the Text and the Checkbox is reduced. 我想删除它(特别是在顶部),以便减少Text和Checkbox之间的空间。

Here is a picture when I have padding="-5dp": 这是我填充=“ - 5dp”时的图片:

Checkbox selected: 选中复选框: 在此输入图像描述

PS: If someone knows a solution with just a Checkbox and it's Text-field, so I can removed the LineairLayout and TextView, while still having the Text above the Checkbox I would also appreciate it. PS:如果有人知道只有一个Checkbox和它的Text-field的解决方案,那么我可以删除LineairLayout和TextView,同时仍然在Checkbox上面的Text我也会很感激。 A single Checkbox with a custom style and Text is better for performance than a Checkbox inside a LineairLayout with an additional TextView. 具有自定义样式和文本的单个复选框比具有附加TextView的LineairLayout内的复选框更适合于性能。 (I know this is more something for a different question, but I just add it here since it doesn't have a lot of priority. Everything is working already, except for the padding of the Checkbox.) (我知道这更像是一个不同的问题,但我只是在这里添加它,因为它没有很多优先级。除了Checkbox的填充之外,一切都已经工作了。)

Ok, I changed the LineairLayout to a RelativeLayout, and than I can change the margin to a minus dp. 好吧,我将LineairLayout更改为RelativeLayout,而且我可以将边距更改为减去dp。 This is my end result: 这是我的最终结果:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="@dimen/default_margin">

    <TextView
        android:id="@+id/tv_edit_checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:singleLine="false"
        android:gravity="center_horizontal"
        android:text="@string/edit_checkbox" />

    <CheckBox
        android:id="@+id/cb_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_edit_checkbox"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="-10dp"
        android:checked="true" />

</RelativeLayout>

This gives the following result: 这给出了以下结果:

RelativeLayout selected: RelativeLayout选择: 在此输入图像描述

This does change the space between the Text and the Checkbox. 这确实会改变Text和Checkbox之间的空间。 For me this is what I want, however it still doesn't explain why the Android Checkbox' internal Image got a padding at its Top, Left and Bottom sides.. Now idea who of Android had this idea, but it was a pretty bad one.. It was better to just use no padding on the internal image and then have some default internal Margin/Padding that can be changed by users.. 对我来说这就是我想要的,但它仍然无法解释为什么Android Checkbox的内部图像在其顶部,左侧和底部都有填充物。现在想知道Android有谁有这个想法,但它是一个非常糟糕的一个..最好只在内部图像上使用无填充,然后有一些默认的内部边距/填充,可由用户更改..

OR 要么

Edit : You can also get the same result with LinearLayout : 编辑:您还可以使用LinearLayout获得相同的结果:

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:padding="0dp"
            android:text="For all products"
            android:textSize="10sp" />

        <CheckBox
            android:id="@+id/checkBox_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="-10dp"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:padding="0dp" />
    </LinearLayout>

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

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