简体   繁体   中英

How to Align LinearLayout to Bottom of Activity

I have the following activity_main.xml file, and I want to align the three buttons inside the LinearLayout to the bottom of the page. I've tried layout_alignParentBotton="true" but that doesn't work.. is there any setting that would let me bring the linearLayout to the bottom of the activity? thanks

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="@color/blueBackground"
    tools:context="org.pctechtips.george.dailyquotes.MainActivity">

        <TextView
            android:id="@+id/main_title"
            android:layout_width="368dp"
            android:layout_height="wrap_content"
            android:text="Daily Quotes"
            android:textSize="30dp"
            android:textColor="@color/whiteText"
            android:textAlignment="center"
            tools:layout_editor_absoluteX="8dp"
            tools:layout_editor_absoluteY="16dp" />

        <ImageView
            android:id="@+id/main_image"
            android:layout_width="100dp"
            android:layout_height="100dp"
            app:srcCompat="@mipmap/ic_launcher_round"
            android:layout_centerHorizontal="true"
            tools:layout_editor_absoluteY="89dp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent" />


        <TextView
            android:id="@+id/quote_text"
            android:layout_width="368dp"
            android:layout_height="wrap_content"
            android:text="This is a random text"
            android:textColor="@color/whiteText"
            android:textSize="20dp"
            tools:layout_editor_absoluteX="8dp"
            tools:layout_editor_absoluteY="246dp" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_alignParentBottom="true">

            <ImageView
                android:id="@+id/previous_quote"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:srcCompat="@android:drawable/ic_media_previous"
                tools:layout_editor_absoluteX="8dp"
                tools:layout_editor_absoluteY="473dp" />

            <ImageView
                android:id="@+id/share_quote"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                tools:layout_editor_absoluteX="8dp"
                tools:layout_editor_absoluteY="473dp" />

            <ImageView
                android:id="@+id/next_quote"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:srcCompat="@android:drawable/ic_media_next"
                tools:layout_editor_absoluteX="8dp"
                tools:layout_editor_absoluteY="473dp" />

        </LinearLayout>
</android.support.constraint.ConstraintLayout>

Change android:layout_alignParentBottom="true" to app:layout_constraintBottom_toBottomOf="parent" . Here is an example that also stretches that layout to the width of the parent and centers the views within it

...
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="parent">
...

Add following Constraints to Your Linear layout .

 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintStart_toStartOf="parent"

Some of your views are not constraints properly. If you want to use RelativeLayout then try the solution below .

<?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"
    android:background="@color/blueBackground"
    tools:context="org.pctechtips.george.dailyquotes.MainActivity">

    <TextView
        android:id="@+id/main_title"
        android:layout_width="368dp"
        android:layout_height="wrap_content"
        android:text="Daily Quotes"
        android:textSize="30dp"
        android:textAlignment="center"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="16dp" />

    <ImageView
        android:id="@+id/main_image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_below="@+id/main_title"
        app:srcCompat="@mipmap/ic_launcher_round"
        android:layout_centerHorizontal="true"
        />


    <TextView
        android:id="@+id/quote_text"
        android:layout_width="match_parent"
        android:layout_below="@+id/main_image"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:text="This is a random text"
        android:textSize="20dp"

        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true">

        <ImageView
            android:id="@+id/previous_quote"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@android:drawable/ic_media_previous"
            />

        <ImageView
            android:id="@+id/share_quote"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

        <ImageView
            android:id="@+id/next_quote"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@android:drawable/ic_media_next"
            />

    </LinearLayout>
</RelativeLayout>

将父约束布局替换为RelativeLayout

Try editing your layout this way:

<?xml version="1.0" encoding="utf-8"?>     <LinearLayout 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:orientation="vertical" android:background="@color/blueBackground" tools:context="org.pctechtips.george.dailyquotes.MainActivity"> 
<LinearLayout android: layout_weight="1" android orientation="vertical" android:layout_width="match_parent" android:layout_height="0dp">
<TextView android:id="@+id/main_title" android:layout_width="368dp" android:layout_height="wrap_content" android:text="Daily Quotes" android:textSize="30dp" android:textColor="@color/whiteText" android:textAlignment="center" /> <ImageView android:id="@+id/main_image" android:layout_width="100dp" android:layout_height="100dp" app:srcCompat="@mipmap/ic_launcher_round" /> <TextView android:id="@+id/quote_text" android:layout_width="368dp" android:layout_height="wrap_content" android:text="This is a random text" android:textColor="@color/whiteText" android:textSize="20dp"/>
</LinearLayout>
 <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_alignParentBottom="true"> <ImageView android:id="@+id/previous_quote" android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@android:drawable/ic_media_previous"/> <ImageView android:id="@+id/share_quote" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ImageView android:id="@+id/next_quote" android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@android:drawable/ic_media_next"/> </LinearLayout> </LinearLayout>

Note the layout weight in the Linear Layout.

Your root layout is a ConstraintLayout so you should position its children using the appropriate constraints listed in the documentation . In order to position the LinearLayout at the bottom center and wrap its content you should use the following:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constrainedWidth="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">

You can also optimize your layout by removing the LinearLayout altogether and constraining the three ImageViews in a horizontal chain as direct children of the root ConstraintLayout like this:

<ImageView
    android:id="@+id/previous_quote"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@android:drawable/ic_media_previous"
    app:layout_constraintHorizontal_chainStyle="packed"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@id/share_quote"
    app:layout_constraintStart_toStartOf="parent" />

<ImageView
    android:id="@+id/share_quote"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@id/next_quote"
    app:layout_constraintStart_toEndOf="@id/previous_quote"/>

<ImageView
    android:id="@+id/next_quote"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@android:drawable/ic_media_next"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@id/share_quote" />

Try this..

<?xml version="1.0" encoding="utf-8"?><?xml version="1.0" encoding="utf- 
8"?>
<android.support.constraint.ConstraintLayout 
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="@android:color/holo_blue_light">

<TextView
    android:id="@+id/main_title"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Daily Quotes"
    android:textAlignment="viewStart"
    android:textColor="@android:color/white"
    android:textSize="30dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

<ImageView
    android:id="@+id/main_image"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_centerHorizontal="true"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:srcCompat="@mipmap/ic_launcher_round"
    tools:layout_editor_absoluteY="89dp" />


<TextView
    android:id="@+id/quote_text"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:text="This is a random text"
    android:textColor="@android:color/white"
    android:textSize="20dp"
    app:layout_constraintTop_toBottomOf="@id/main_image" />

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="horizontal"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">

    <ImageView
        android:id="@+id/previous_quote"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@android:drawable/ic_media_previous" />

    <ImageView
        android:id="@+id/share_quote"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/next_quote"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@android:drawable/ic_media_next" />

</LinearLayout>
</android.support.constraint.ConstraintLayout>

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