简体   繁体   中英

How to move view to the end in LinearLayout

I don't know how to move my ImageView to the right, now it looks like that

在此输入图像描述

And I'd like to have something like that, so the image will stick to the right side of this CardView

在此输入图像描述

<android.support.v7.widget.CardView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    app:cardCornerRadius="6dp"
    app:cardElevation="6dp"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Test"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Test"/>

        </LinearLayout>

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="50dp"
            android:layout_height="50dp"
            app:srcCompat="@mipmap/ic_launcher" />


    </LinearLayout>

</android.support.v7.widget.CardView>

You will get the desired result with a RelativeLayout :

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_toStartOf="@id/imageView3">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Test"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Test"/>

    </LinearLayout>

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentEnd="true"
        app:srcCompat="@mipmap/ic_launcher" />


</RelativeLayout>

RelativeLayout is designed for these cases. As you can see it takes only an attribute like layout_alignParentEnd to achieve what you want

Try this:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        app:cardCornerRadius="6dp"
        app:cardElevation="6dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="6"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Test" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Test" />

            </LinearLayout>

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="1"
                app:srcCompat="@mipmap/ic_launcher" />

        </LinearLayout>

    </android.support.v7.widget.CardView>

LinearLayouts have a weight property that allows you to define how much surface area individual child views can take up. You assign a "weight" to child views and "heavier" children take up more space in the parent view.

Note : As others as stated, it's better and easier (and probably more efficient) to use a RelativeLayout , since it is better suited for aligning child views. Also, when using weight if aligning horizontally, it's usually wise to keep layout_width at 0dp and if aligning vertically, keep layout_height at 0dp.

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