简体   繁体   English

如何在LinearLayout中将视图移动到最后

[英]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 我不知道如何向右移动我的ImageView ,现在它看起来像那样

在此输入图像描述

And I'd like to have something like that, so the image will stick to the right side of this CardView 而且我想有类似的东西,所以图像会粘在这个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获得所需的结果:

<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. RelativeLayout专为这些案例而设计。 As you can see it takes only an attribute like layout_alignParentEnd to achieve what you want 正如您所看到的,它只需要像layout_alignParentEnd这样的属性来实现您想要的效果

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. LinearLayouts具有权重属性,允许您定义单个子视图可占用的表面积。 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. 注意 :正如其他人所说,使用RelativeLayout更好,更容易(也可能更有效),因为它更适合于对齐子视图。 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. 此外,当使用权重进行水平对齐时,通常明智的做法是将layout_width保持在0dp,如果垂直对齐,则将layout_height保持为0dp。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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