簡體   English   中英

如何在LinearLayout中將視圖移動到最后

[英]How to move view to the end in LinearLayout

我不知道如何向右移動我的ImageView ,現在它看起來像那樣

在此輸入圖像描述

而且我想有類似的東西,所以圖像會粘在這個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>

您將使用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這樣的屬性來實現您想要的效果

嘗試這個:

<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具有權重屬性,允許您定義單個子視圖可占用的表面積。 您為子視圖指定了“權重” ,而“較重”的子視圖在父視圖中占用了更多空間。

注意 :正如其他人所說,使用RelativeLayout更好,更容易(也可能更有效),因為它更適合於對齊子視圖。 此外,當使用權重進行水平對齊時,通常明智的做法是將layout_width保持在0dp,如果垂直對齊,則將layout_height保持為0dp。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM