如何将复选框与文本对齐到屏幕右侧?

时间:2016-08-02 10:52:13

标签: android checkbox

我在TableRow内有一个复选框。我想将复选框和文本对齐到屏幕的右侧,但是当我使用android:gravity="right"时,它只会将文本对齐到屏幕的右侧而不是正方形(复选框本身)。它们似乎是分开对齐的(左侧是复选框,屏幕右侧是文本)。

以下是xml的片段,它引用了复选框:

<TableRow android:layout_height="wrap_content"
          android:layout_width="match_parent"
          android:layout_marginBottom="10dp">

          <CheckBox
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Select"
                android:id="@+id/checkboxSelect"
                android:layout_column="0"
                android:layout_weight="1"
                android:gravity="right"/>
</TableRow>

如何将复选框及其上的文本对齐到屏幕右侧?

提前致谢!

5 个答案:

答案 0 :(得分:5)

只需添加Checkbox,如下所示。

<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
    android:text="hello"
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:button="@null"
    android:drawableRight="?android:attr/listChoiceIndicatorMultiple"/>
  

它会在左侧显示text,在右侧显示check box

答案 1 :(得分:2)

属性android:gravity正在处理视图本身,而android:layout_gravity建议父视图对齐它的子视图。

如果你想告诉父布局在左边或右边对齐它的孩子,你有两个选择:

  1. 在父级上使用android:gravity,因此父级将尝试在该严重性下对齐所有子视图。
  2. 子视图上的
  3. android:layout_gravity,只有这个视图会尝试在重力中对齐。

答案 2 :(得分:0)

试试这个。您将获得右侧的文本和复选框

<TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginBottom="10dp"
        android:gravity="right">

        <CheckBox
            android:id="@+id/checkboxSelect"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:gravity="right|center_vertical"
            android:text="Select" />
    </TableRow>

答案 3 :(得分:0)

尝试使用RelativeLayout

<RelativeLayout 
      android:layout_height="wrap_content"
      android:layout_width="match_parent"
      android:layout_marginBottom="10dp">

      <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/text_view"
            android:id="@+id/checkboxSelect"/>

      <TextView 
            android:id="@+id/text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"/>
</RelativeLayout>

答案 4 :(得分:0)

试试这个

    <CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center|end"
    android:layout_gravity="start"
    android:button="@null"
    android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
    android:text="hello" />

设置 android:button="@null" 和 设置这个 drawable "?android:attr/listChoiceIndicatorMultiple" drawableRight 或 drawableLeft 随心所欲。