简体   繁体   中英

How can I align a TextView to the far left and ImageView to the far right in an Actionbar Spinner?

I am trying to layout an actionbar spinner dropdown list. I have a textview and an imageview in each row. I want the textview to align to the far left of the dropdown (with the paddingLeft) and I want the imageview to align to the far right of the dropdown. The text in the textviews varies in length, so the icons end up being aligned to the immediate right of the textviews. This layout provides a two line item on the actionbar with the spinner title and selected item name.

Here is my current layout:

<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>

I've tried a relative layout like below, with similar results (see screen shot). The bluetooth icon is a stand in for the yet to be created icon (same size):

<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>

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM