如何在LinearLayout中将视图移动到末尾

7
我不知道如何把我的ImageView向右移动,现在它看起来像这样:

enter image description here

我希望它能像这样,所以图片将会粘附在这个CardView的右边:

enter image description here

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

2
尝试使用RelativeLayout代替。 - aminography
2个回答

10
使用 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 是为这些情况而设计的。如您所见,它只需要一个像 layout_alignParentEnd 这样的属性即可实现您想要的效果。


2

试试这个:

<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有一个weight属性,允许您定义每个子视图可以占用多少表面积。您为子视图分配一个"weight""更重的"子视图在父视图中占用更多的空间。 注意:正如其他人所述,最好、最容易(也可能更有效)使用RelativeLayout,因为它更适合对齐子视图。此外,如果水平对齐,则使用权重时通常应将layout_width保持为0dp,如果垂直对齐,则应将layout_height保持为0dp。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接