如何将两个视图放在 LinearLayout 的另一侧

How to place two views on opposite side of LinearLayout

如何将 ImageButton 放在屏幕的右侧?

我认为它与“layout_gravity”属性有关,但我不知道该怎么做。 这是创建每一行的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/list_generated_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="20dp"
        android:layout_gravity="left|center_vertical" />

    <ImageButton
        android:id="@+id/list_generated_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:src="@drawable/ic_refresh"/>

</LinearLayout>

只需在文本视图中添加一行

android:layout_weight="1"

那就一切如你所愿

使用这个

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

    <TextView
        android:id="@+id/list_generated_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:layout_weight="1"
        android:textSize="20dp"
        android:layout_gravity="left|center_vertical" />

    <ImageButton
        android:id="@+id/list_generated_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:src="@drawable/ic_refresh"/>

</LinearLayout>