views:

408

answers:

1

I am working on a fairly basic screen layout for my first Android application and running into some issues. My goal is to have a TextView in the top left and top right corner, a large "hello world" TextView in the exact middle of the screen, and then a button at the bottom of the screen. My issue is, to center the "hello world" TextView vertically, I need to set the layout_height="fill_parent". However, this causes the middle TextView to cover and hide the button at the bottom of the screen. Is there a better way to do this than what I am currently trying? I have attached my ui xml below. Thanks!

<?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_horizontal">
    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="1">
        <TableRow>
            <TextView
                android:text="@string/label_left" />
            <TextView
                android:text="@string/label_right"
                android:gravity="right" />
        </TableRow>
    </TableLayout>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="@string/hello_world"
        android:gravity="center"
        android:textSize="40sp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/next_activity"
        android:gravity="center"
        android:textSize="30sp" />      
</LinearLayout>
+4  A: 

If you use a RelativeLayout instead, you can align other views to the top/bottom of the screen easily:

use android:layout_alignParentTop="true"

or android:layout_alignParentBottom="true"

same for left/right :

android:layout_alignParentLeft="true" or android:layout_alignParentRight="true"

What about this ?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="fill_parent" >

<TextView
    android:text="@string/label_left" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true"/>
<TextView
    android:text="@string/label_right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="right" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentRight="true"/>

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"
    android:gravity="center"
    android:textSize="40sp" 
    android:layout_centerInParent="true"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/next_activity"
    android:gravity="center"
    android:textSize="30sp" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true"/>      

Is it what you are looking for ?

Hubert
That did it perfectly. Thanks!
Josh