To align views at the bottom of the screen

Asked 2 years ago, Updated 2 years ago, 164 views

First of all, this is the layout code I wrote:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView android:text="@string/welcome" 
        android:id="@+id/TextView" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content">
    </TextView>

    <LinearLayout android:id="@+id/LinearLayout" 
        android:orientation="horizontal"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:gravity="bottom">

            <EditText android:id="@+id/EditText" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content">
            </EditText>

            <Button android:text="@string/label_submit_button" 
                android:id="@+id/Button" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </Button>

    </LinearLayout>

</LinearLayout>

The result of this layout is the same as the image on the left, but I want to make it like the one on the right.

Of course, the most obvious answer would be to set the TextView height to fill_parent, but this would leave no space for the buttons and input compartments. The original question is about the button and input space being located at the bottom with a fixed height, and the text view filling the rest of the space, similarly in the horizontal linear layout, the button is fixed to fit the content, and the input space filling the rest is also curious in the same context.

If the first item in Linear Layout is set to fill_parent, it fills the parent layout exactly without leaving any space for other items, what should we do to make sure that the first item fills only the rest of the space except for the minimum space that the rest of the items need?

android-layout user-interface android xml

2022-09-22 21:31

1 Answers

I think you should use relative layout.

If you have a relative layout that fills the entire screen, you can position the button at the bottom of the screen using android:layout_alignParentBottom.

If you still can't see the view you placed at the bottom, the layout at the top will occupy the entire space. In this case, place the view to be placed at the bottom on the layout first, and place the remaining views using android:layout_above. This allows the view at the bottom to take up as much space as needed, and allows other views to take up the remaining space.


2022-09-22 21:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.