为什么复选框和文本之间会有间隙?

5
我正在使用图像作为复选框。这是我正在使用的XML。
<CheckBox
    android:id="@+id/remember"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"        
    android:layout_below="@+id/tableLayout1"
    android:button="@drawable/checkbox_selector"
    android:layout_marginLeft="52dp"
    android:text="@string/remember"
    android:textColor="#000000" />

我发现图片和文字之间有间隙(如下图所示),这是默认设置吗?如果可以更改,请告诉我需要添加哪个属性。

checkbox_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" 
    android:drawable="@drawable/checked" />
<item android:state_checked="false" 
    android:drawable="@drawable/un_checked" />
</selector>

enter image description here


你使用的是哪种布局?假设你正在使用相对布局(或)线性布局,请检查你正在使用的填充是什么。通过调整填充,我猜你可以解决这个问题。 - kosa
我正在使用相对布局,但该布局没有填充。 - RajaReddy PolamReddy
添加TextView的定义。 - Nick Campion
@NickCampion 我仍然在顶部和右侧看到间隙。 - RajaReddy PolamReddy
老实说,诊断这些问题的最佳方法是在模拟器上打开应用程序并使用层次结构查看器进行连接。然后,您可以通过点击元素和切换“额外”选项来查看是谁给您的视图添加了额外的间距。我猜测有两个问题。1)文本视图以文本基线对齐,而您的复选框则根据视图中心对齐;2)文本视图可能会受到重力或默认文本视图边距的影响。同样,验证所有这些的最简单方法是通过内置于您的eclipse中的层次结构查看器工具。 - Nick Campion
3个回答

7

2
使用以下代码来关闭间隙。
 final float scale = this.getResources().getDisplayMetrics().density;
 checkBox.setPadding(checkBox.getPaddingLeft() - (int)(10.0f * scale + 0.5f),
    checkBox.getPaddingTop(),
    checkBox.getPaddingRight(),
    checkBox.getPaddingBottom());

0
请尝试这个:
    <RelativeLayout  android:layout_width="wrap_content"
           android:layout_height="wrap_content" 
           android:gravity="center">
         <CheckBox
             android:id="@+id/checkBox1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:button="@drawable/checkbox_selector"
             android:text="" />
         <TextView
             android:id="@+id/textView1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="Remember Me"
             android:layout_toRightOf="@+id/checkBox1"
             android:layout_centerInParent="true
             android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接