复选框是文本,如何添加填充?

时间:2011-09-11 22:09:07

标签: java android xml

我有一个带有复选框的列表视图:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <CheckBox android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:id="@+id/nomeAPP" style="?listItem" />

</LinearLayout>

但问题是:复选框是在文本上(文本是左边的很多)

我该如何纠正?

4 个答案:

答案 0 :(得分:1)

如何将android:paddingLeft="xdp"放在哪里x是您需要为文本添加的dp数?

填充填充只会移动文本而不会移动“Tick-Button”。 如果使用硬编码的dp不符合您的需要,您也可以考虑android:gravity属性。

答案 1 :(得分:1)

如果您的文字与复选框的布局相同,请执行以下操作:

<CheckBox 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" 
      android:id="@+id/nomeAPP" 
      style="?listItem"
      android:layout_marginLeft="10dp" />

但如果不是,那就这样做:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:paddingLeft="10dp">

    <CheckBox 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:id="@+id/nomeAPP" 
        style="?listItem" />

</LinearLayout>

Obs:我只为示例添加了左边距和填充。

如果您有更多疑问,请告诉我。感谢。

答案 2 :(得分:0)

这是我用于列表中项目的内容,每个项目都有文本和单选按钮。请注意layout_weight和layout_gravity设置:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    >
        <TextView
            android:id="@+id/select_file_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:textSize="16sp"
            android:layout_weight="100"
        />
        <RadioButton
            android:id="@+id/file_item_checked"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:textSize="16sp"
            android:layout_gravity="right"
        />
    </LinearLayout>

答案 3 :(得分:0)

有两种方法可以解决此问题_

1通过扩展android.widget.CheckBox类来构建您自己的自定义CheckBox类。完整自定义代码如下_

public class CustomCheckBox extends CheckBox {

public CustomCheckBox(Context context) {
    super(context);
}

public CustomCheckBox(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public CustomCheckBox(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public int getCompoundPaddingLeft() {
    final float scale = this.getResources().getDisplayMetrics().density;
     /*add padding you needed so the text is not on top of the
      * CheckBox.
      */
    return (super.getCompoundPaddingLeft() + (int) (10.0f * scale + 0.5f));
}
} 

2在您的活动中设置填充_

 final float scale =this.getResources().getDisplayMetrics().density;

 checkBox.setPadding(checkBox.getPaddingLeft() + (int)(10.0f * scale + 0.5f),
    checkBox.getPaddingTop(),
    checkBox.getPaddingRight(),
    checkBox.getPaddingBottom());

我希望这能帮助所有有这个问题的人...... :)

相关问题