[英]How to remove internal Image padding (Top, Left and Bottom) of an Android Checkbox
我在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>
然而,Checkbox的问题在于它的内部“图像”周围有一个看不见的边框。 如何删除? 我已经尝试过android:padding="-5dp"
但这只适用于CheckBox的右侧,出于某种原因,而不是顶部,左侧和底部(我的主要关注点是Top)。
以下是Eclipse Graphical Layout窗口中Checkbox的屏幕截图:
选择LineairLayout: 选中复选框:
在第二张图片中,您可以看到Checkbox在您选择它时会在其周围添加一些填充。 我想删除它(特别是在顶部),以便减少Text和Checkbox之间的空间。
这是我填充=“ - 5dp”时的图片:
选中复选框:
PS:如果有人知道只有一个Checkbox和它的Text-field的解决方案,那么我可以删除LineairLayout和TextView,同时仍然在Checkbox上面的Text我也会很感激。 具有自定义样式和文本的单个复选框比具有附加TextView的LineairLayout内的复选框更适合于性能。 (我知道这更像是一个不同的问题,但我只是在这里添加它,因为它没有很多优先级。除了Checkbox的填充之外,一切都已经工作了。)
好吧,我将LineairLayout更改为RelativeLayout,而且我可以将边距更改为减去dp。 这是我的最终结果:
<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>
这给出了以下结果:
RelativeLayout选择:
这确实会改变Text和Checkbox之间的空间。 对我来说这就是我想要的,但它仍然无法解释为什么Android Checkbox的内部图像在其顶部,左侧和底部都有填充物。现在想知道Android有谁有这个想法,但它是一个非常糟糕的一个..最好只在内部图像上使用无填充,然后有一些默认的内部边距/填充,可由用户更改..
要么
编辑:您还可以使用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.