将复选框对齐LinearLayout android中的右侧

And*_*w_Z 2 android android-layout android-linearlayout

我正在尝试使用LinearLayout中的复选框创建一些文本视图.我想得到这样的结果:

TextView           Ckeckbox
TextViewwwwww      Ckeckbox
TextV              Ckeckbox
Run Code Online (Sandbox Code Playgroud)

但目前我有这个:

TextView  Checkbox
TextViewwwww  Checkbox
TextView  Checkbox
Run Code Online (Sandbox Code Playgroud)

这是我的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:orientation="horizontal">

  <TextView
    android:id="@+id/itemTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
 <CheckBox
        android:id="@+id/checkItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

而这个xml给了我相同的结果:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:orientation="horizontal">

  <TextView
    android:id="@+id/itemTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="start"/>
 <CheckBox
        android:id="@+id/checkItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="end"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Jos*_*que 6

您可以使用您的layout_weight属性TextView来自动指定TextView填充CheckBox不占用的任何空间.例如,以下内容将您的复选框设置为结尾(前提是它们没有与之关联的文本),然后让TextView填充其余的布局.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/itemTitle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    <CheckBox
        android:id="@+id/checkItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

此外,你应该使用match_parent,而不是fill_parent作为fill_parent已被弃用.