Which layout should I select?

I am coming back to Android after a really long absence and it turns out, I've pretty much forgotten everything.

I want to a build a UI like this:

在这里输入图像描述

The buttons Item1 and Item2 trigger a move to another page, while the Perform Action button does an action in place. And the ad should be docked to the bottom of the screen.

What layout should I pick to achieve this UI? I am targeting Android 2.2 (if that matters).


I believe going for a Relative Layout helps. May be this can help, although you might want to reshuffle things a bit.

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="42dp"
            android:text="@string/item1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/button1"
            android:layout_below="@+id/button1"
            android:layout_marginTop="48dp"
            android:text="@string/item2" />

        <Button
            android:id="@+id/button4"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="@string/ad_goes_here" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/button2"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="62dp"
            android:text="@string/perform_action" />

    </RelativeLayout>

I think using Linear Layout is best


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

<Button
 android:id="@+id/btn1"
 android:text="Item1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:layout_gravity="center"
 />

<Button
 android:id="@+id/btn2"
 android:text="Item2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:layout_gravity="center"
 />
<Button
 android:id="@+id/btn3"
 android:text="Perform Action"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:layout_gravity="center"
 />

<Button
 android:id="@+id/btn4"
 android:text="Ad Goes here"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:layout_gravity="center"
 />

</LinearLayout>

You can use a LinearLayout with android:orientation="vertical" . However there are many other layouts which are able to achieve your desired UI. I prefer to just stick to LinearLayout because I could just set it to either vertical or horizontal.

链接地址: http://www.djcxy.com/p/93220.html

上一篇: 如何在屏幕底部设置TextView

下一篇: 我应该选择哪种布局?