简体   繁体   中英

LinearLayout at the top and at the bottom of the screen

I have two different LinearLayouts inside one LinearLayout and I would like the first layout to be at the top and the second layout to be at the bottom. I tried with android:gravity="top" but it didn't work. Here is my xml code:

<?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" android:gravity="center">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:gravity="top">
    <ImageView android:layout_height="wrap_content" android:id="@+id/imageView1"
        android:src="@drawable/icon" android:layout_width="wrap_content"></ImageView>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="wrap_content">
        <TableRow android:weightSum="2">
            <Button android:text="Button" android:id="@+id/button2"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_weight="1"></Button>
            <Button android:text="Button" android:id="@+id/button1"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_weight="1"></Button>
        </TableRow>
        <TableRow android:weightSum="2">
            <Button android:text="Button" android:id="@+id/button3"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_weight="1"></Button>
            <Button android:text="Button" android:id="@+id/button4"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_weight="1"></Button>
        </TableRow>
    </TableLayout>
</LinearLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:gravity="bottom">
    <ImageView android:layout_height="wrap_content" android:id="@+id/imageView1"
        android:src="@drawable/icon" android:layout_width="wrap_content"></ImageView>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="wrap_content">
        <TableRow android:weightSum="2">
            <Button android:text="Button" android:id="@+id/button2"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_weight="1"></Button>
            <Button android:text="Button" android:id="@+id/button1"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_weight="1"></Button>
        </TableRow>
        <TableRow android:weightSum="2">
            <Button android:text="Button" android:id="@+id/button3"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_weight="1"></Button>
            <Button android:text="Button" android:id="@+id/button4"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_weight="1"></Button>
        </TableRow>
    </TableLayout>
</LinearLayout>

You could simply add :

<LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:layout_weight="1" />

between your top and bottom LinearLayout (and remove the gravity from the parent container).

change you parent layout as RelativeLayout instead of LinearLayout , and

for top of the parent layout set following in child layout

android:layout_alignParentTop="true"

and for bottom of the parent layout set following in child layout

android:layout_alignParentBottom="true"

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