简体   繁体   English

在RelativeLayout的底部对齐LinearLayout

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

I have these nested layouts and i want to put a LinearLayout anchored to the bottom (internally) of the first RelativeLayout but i don't know how to do it. 我有这些嵌套的布局,我想将LinearLayout锚定到第一个RelativeLayout的底部(内部),但是我不知道该怎么做。 Can someone help me? 有人能帮我吗? I want something like this: https://imgur.com/eCCYS8Q 我想要这样的东西: 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>

If you want to align a view to the bottom of a relative layout you need to use android:layout_alignParentBottom="true" 如果要使视图与相对布局的底部对齐,则需要使用android:layout_alignParentBottom="true"

in the view that is going to be stuck at the bottom of the relative 在视图中将被卡在相对对象的底部

Since you need weight too in your UI so This will work for you 由于您的用户界面也需要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>

Align LinearLayout to the bottom using id. 使用id将LinearLayout对齐到底部。 take the id of relative layout and then align LinearLayout to the bottom at the end of relative layout id. 取得相对布局ID,然后将LinearLayout与相对布局ID的底部对齐。

To make layout_weight work properly set android:layout_height="0dp" for both relative layouts 为了使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>

change 10dp to whatever suits you. 将10dp更改为适合您的值。
I set the colors just to distinguish the layouts 我设置颜色只是为了区分布局

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

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