Android 在Actionbar微调器中,如何将TextView与最左侧对齐,将ImageView与最右侧对齐?

Android 在Actionbar微调器中,如何将TextView与最左侧对齐,将ImageView与最右侧对齐?,android,android-layout,layout,android-actionbar,spinner,Android,Android Layout,Layout,Android Actionbar,Spinner,我正在尝试布局一个actionbar微调器下拉列表。我每行有一个文本视图和一个图像视图。我希望textview与下拉列表的最左侧对齐(使用paddingLeft),而imageview与下拉列表的最右侧对齐。文本视图中的文本长度不同,因此图标最终与文本视图的右侧对齐。此布局在actionbar上提供了一个两行项目,其中包含微调器标题和所选项目名称 以下是我当前的布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/r

我正在尝试布局一个actionbar微调器下拉列表。我每行有一个文本视图和一个图像视图。我希望textview与下拉列表的最左侧对齐(使用paddingLeft),而imageview与下拉列表的最右侧对齐。文本视图中的文本长度不同,因此图标最终与文本视图的右侧对齐。此布局在actionbar上提供了一个两行项目,其中包含微调器标题和所选项目名称

以下是我当前的布局:

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

    <TextView
        android:id="@+id/spinner_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp" />

    <LinearLayout
        android:id="@+id/item_row"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="-5dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/item_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:layout_weight="1"
            android:gravity="left"
            android:textSize="18sp" />

        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"
            android:contentDescription="icon" />
    </LinearLayout>
</LinearLayout>

我尝试了如下的相对布局,得到了类似的结果(参见屏幕截图)。蓝牙图标是尚未创建的图标(大小相同)的替代:



使用
RelativeLayout
而不是
LinearLayout
,并在您的孩子身上使用alignParentLeft/Right属性来正确放置他们。

您可以发布您尝试的RelativeLayout吗?我已将其添加到原始问题中删除该行
android:layout_-toRightOf=“@+id/item_-name”
。这与
alignParentRight
的组合告诉系统将图像的开头直接放在文本的右侧,并将结尾放在容器的右侧。你可以在截图中看到这一点;图像将占用所有剩余空间。或者,如果要避免文本和图像重叠,可以告诉文本视图将其自身布局到imageview的左侧。这将使textview占用剩余的空间。希望有帮助!
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/spinner_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="14sp" />

<RelativeLayout
    android:id="@+id/item_row"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:textSize="18sp" />

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/item_name"
        android:contentDescription="icon" />
</RelativeLayout>