Android中心布局隐藏按钮
问题描述:
我正在为我的第一个Android应用程序进行相当基本的屏幕布局,并遇到一些问题。我的目标是在左上角和右上角有一个TextView,屏幕中间有一个大的“hello world”TextView,然后是屏幕底部的一个按钮。我的问题是,为了垂直居中“hello world”TextView,我需要设置layout_height =“fill_parent”。但是,这会导致中间的TextView覆盖并隐藏屏幕底部的按钮。有没有比我目前尝试做的更好的方法?我在下面附上了我的ui xml。谢谢!Android中心布局隐藏按钮
<?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>
答
如果使用的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"/>
是不是你在找什么?
这样做完美。谢谢! – Blather 2010-03-06 15:46:20