简体   繁体   中英

Android - swap views in LinearLayout via xml

Here is my xml code, it has an ImageView which stands before LinearLayout id = "linearLayout2" :

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

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout1"
        android:background="@drawable/border"
        android:paddingBottom="5dp">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView"
            android:src="@drawable/ic_swap"
            android:layout_gravity="center_vertical" />

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout2"
            android:paddingBottom="5dp"
            android:layout_marginRight="5dp">

            <AutoCompleteTextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/fromStationField"
                android:completionThreshold="1"
                android:hint="From"
                android:textAlignment="textStart"
                android:gravity="left|start"
                android:layout_gravity="left" />

            <AutoCompleteTextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/toStationField"
                android:completionThreshold="1"
                android:hint="To"
                android:background="@android:color/transparent"
                android:paddingRight="4dp"
                android:paddingLeft="4dp" />

        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

What I want to do is to make an ImageView stands after the LinearLayout id = "linearLayout2" like I show in an image bellow.

How to do this using only xml ?

在此处输入图片说明

One solution could be inside of linearLayout1 to move the ImageView after linearLayout2. Then set linearLayout1's width to match_parent and the sibling views width to 0dp. Lastly, use layout_weight to achieve the proportion of size you want for the ImageView and LinearLayout2.

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

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/border"
        android:orientation="horizontal"
        android:paddingBottom="5dp"
        android:weightSum="10">

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_weight="9"
            android:orientation="vertical"
            android:paddingBottom="5dp">

            <AutoCompleteTextView
                android:id="@+id/fromStationField"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:completionThreshold="1"
                android:gravity="left|start"
                android:hint="From"
                android:textAlignment="textStart" />

            <AutoCompleteTextView
                android:id="@+id/toStationField"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:completionThreshold="1"
                android:hint="To"
                android:paddingLeft="4dp"
                android:paddingRight="4dp" />

        </LinearLayout>

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:src="@android:drawable/ic_swap" />

    </LinearLayout>
</RelativeLayout>

In the layout below, I'm using negative margins in the ImageView and a positive margin in the LinearLayout container left of ImageView so that we don't need to use layout_weight property and can keep using match_parent . For this you need to fix the size of your ImageView . Check this code out:

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

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout1"
        android:background="@drawable/border"
        android:paddingBottom="5dp">



        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout2"
            android:layout_marginRight="55dp"
            android:paddingBottom="5dp">

            <AutoCompleteTextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/fromStationField"
                android:completionThreshold="1"
                android:hint="From"
                android:textAlignment="textStart"
                android:gravity="left|start"
                android:layout_gravity="left" />

            <AutoCompleteTextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/toStationField"
                android:completionThreshold="1"
                android:hint="To"
                android:background="@android:color/transparent"
                android:paddingRight="4dp"
                android:paddingLeft="4dp" />

        </LinearLayout>

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/imageView"
            android:src="@drawable/ic_swap"
            android:layout_marginLeft="-55dp"
            android:layout_gravity="center_vertical" />

    </LinearLayout>
</RelativeLayout>

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