1

我正在为我的第一个 Android 应用程序设计一个相当基本的屏幕布局,并遇到了一些问题。我的目标是在左上角和右上角有一个 TextView,在屏幕正中间有一个大的“hello world”TextView,然后在屏幕底部有一个按钮。我的问题是,要垂直居中“hello world”TextView,我需要设置 layout_height="fill_parent"。但是,这会导致中间的 TextView 覆盖并隐藏屏幕底部的按钮。有没有比我目前正在尝试的更好的方法来做到这一点?我在下面附上了我的 ui xml。谢谢!

<?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

1 回答 1

5

如果您改用 RelativeLayout,您可以轻松地将其他视图对齐到屏幕的顶部/底部:

采用android:layout_alignParentTop="true"

或者android:layout_alignParentBottom="true"

左/右相同:

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

那这个呢 ?

<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"/>      

是你要找的吗?

于 2010-03-06T08:20:54.083 回答