簡體   English   中英

在RelativeLayout的底部對齊LinearLayout

[英]Align a LinearLayout at the bottom of a RelativeLayout

我有這些嵌套的布局,我想將LinearLayout錨定到第一個RelativeLayout的底部(內部),但是我不知道該怎么做。 有人能幫我嗎? 我想要這樣的東西: https : //imgur.com/eCCYS8Q

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical"
        android:weightSum="1">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.65"
            android:background="@drawable/background_gradient">

            <LinearLayout
                android:layout_width="match_parent"

                android:layout_height="wrap_content">

            </LinearLayout>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.35">

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

如果要使視圖與相對布局的底部對齊,則需要使用android:layout_alignParentBottom="true"

在視圖中將被卡在相對對象的底部

由於您的用戶界面也需要weight ,因此這對您有用

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:orientation="vertical"
    android:weightSum="1">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.65"
        android:background="@drawable/background_gradient">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_height="wrap_content">

        </LinearLayout>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.35">

    </RelativeLayout>
</LinearLayout>

使用id將LinearLayout對齊到底部。 取得相對布局ID,然后將LinearLayout與相對布局ID的底部對齊。

為了使layout_weight正常工作,請為兩個相對布局設置android:layout_height="0dp"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.65"
        android:background="@color/colorPrimary">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_marginTop="10dp"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="10dp"
            android:layout_height="match_parent"
            android:background="@color/colorAccent">

        </LinearLayout>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.35"
        android:background="@color/colorPrimaryDark">

    </RelativeLayout>

</LinearLayout>

將10dp更改為適合您的值。
我設置顏色只是為了區分布局

暫無
暫無

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

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