【问题标题】:Why do i get a gap between checkbox and text?为什么我在复选框和文本之间出现空白?
【发布时间】:2012-03-02 14:54:56
【问题描述】:

我正在使用图像作为复选框。 这是我正在使用的 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>

【问题讨论】:

  • 您使用的是哪种布局?假设您使用的是相对(或)线性布局,请检查您使用的填充是什么。通过调整填充我想你可以解决这个问题。
  • 我正在使用相对布局,但该布局没有填充..
  • 添加文本视图的定义
  • @NickCampion 我仍然在顶部和右侧得到差距..
  • 老实说,诊断这些问题的最佳方法是在您的模拟器上打开应用程序并连接到层次结构查看器。然后,您可以通过单击元素并打开和关闭为您的视图提供额外间距的“额外人员”来查看。我的猜测是有两个问题。 1)文本视图在文本基础基线上对齐,而您的复选框基于视图的中心对齐,并且 2)文本视图可能会因重力或默认文本视图填充而倾斜。同样,验证所有这些的最简单方法是通过 Eclipse 中内置的 hierarchyviewer 工具。

标签: android checkbox


【解决方案1】:

使用下面的代码来缩小差距

 final float scale = this.getResources().getDisplayMetrics().density;
 checkBox.setPadding(checkBox.getPaddingLeft() - (int)(10.0f * scale + 0.5f),
    checkBox.getPaddingTop(),
    checkBox.getPaddingRight(),
    checkBox.getPaddingBottom());

【讨论】:

    【解决方案2】:

    尝试弄乱复选框的 android:paddingLeft 属性。

    更多信息请参阅此帖子:Android - Spacing between CheckBox and text

    【讨论】:

      【解决方案3】:

      请试试这个:::::

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-06-23
        • 1970-01-01
        • 2012-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-03
        相关资源
        最近更新 更多