简体   繁体   中英

how to align LinearLayout at the bottom of RelativeLayout inside coordinatorLayout

I have a RelativeLayout and a LinearLayout inside a CoordinatorLayout. I want to align the LinearLayout at the bottom of RelativeLayout. but layoutbelow does not work here. I dont know how to implement this.

this is my code

<android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <RelativeLayout
                android:id="@+id/top"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="150dp"
                    android:src="@drawable/artboard_signup"
                    android:scaleType="fitXY"
                    android:visibility=""
                    />

            </RelativeLayout>



            <LinearLayout
                android:id="@+id/bottom"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginTop="170dp">

                <LinearLayout
                    android:layout_weight="7"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingTop="15dp"
                        android:paddingLeft="10dp"
                        android:paddingRight="10dp"
                        android:text="Top Bootable Antivirus For Your PC?"/>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="8 hours ago"
                        android:paddingLeft="10dp"/>

                </LinearLayout>
        </LinearLayout>
    </android.support.design.widget.CoordinatorLayout>

I want the LinearLayout with id bottom to be at the bottom of the RelativeLayout with id top.

Is there anyway that I could achieve this.

尝试在android:id="@+id/bottom"之后添加android:layout_gravity="bottom" android:id="@+id/bottom"

Use RelativeLayout instead of CoordinateLayout and then add the line "android:layout_alignParentBottom="true" " inside LearLayout.

<?xml version="1.0" encoding="utf-8"?>
<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">
    <RelativeLayout
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="150dp"
            app:srcCompat="@drawable/ic_call_black_24dp"
            android:scaleType="fitXY"
            android:visibility="visible"
            />

    </RelativeLayout>



    <LinearLayout
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="170dp"
        android:layout_alignParentBottom="true">

        <LinearLayout
            android:layout_weight="7"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingTop="15dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:text="Top Bootable Antivirus For Your PC?"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="8 hours ago"
                android:paddingLeft="10dp"/>

        </LinearLayout>
    </LinearLayout>

May be this would work. Try it.

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<RelativeLayout
    android:id="@+id/top"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:scaleType="fitXY"
        android:src="@drawable/artboard_signup"
        android:visibility="" />

    <LinearLayout
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="170dp"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="7"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="10dp"
                android:paddingTop="15dp"
                android:paddingRight="10dp"
                android:text="Top Bootable Antivirus For Your PC?" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="10dp"
                android:text="8 hours ago" />

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

Solution:

Just add a <LinearLayout> inside your <CoordinatorLayout> as shown below:

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        orientation:"vertical">

        <RelativeLayout
            android:id="@+id/top"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:src="@drawable/artboard_signup"
                android:scaleType="fitXY"
                android:visibility=""
                />

        </RelativeLayout>



        <LinearLayout
            android:id="@+id/bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="170dp">

            <LinearLayout
                android:layout_weight="7"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingTop="15dp"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:text="Top Bootable Antivirus For Your PC?"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="8 hours ago"
                    android:paddingLeft="10dp"/>

            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

Hope it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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