2011-06-14 51 views
2

我正在使用这个XML在底部显示我的一个Button,在顶部显示一个TextView,中间显示一个TextView。中间的textview覆盖了按钮和顶部textview之间的整个范围。中间TextView根本没有显示。哪里不对?相对布局,文本视图不显示

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView 
     android:id="@+id/task" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/tasks" 
     /> 
    <Button 
     android:id="@+id/btnAddTask" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/AddTasks" 
     android:layout_alignParentBottom="true" 
     /> 
    <TextView 
     android:id="@+id/TasksList" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/tasks" 
     android:layout_above="@id/task" 
     android:layout_below="@id/btnAddTask" 
     /> 
</RelativeLayout> 
+1

+1用于显示代码标记中的整个代码而不打扰版主。 – 2011-06-14 16:58:33

回答

2

你的问题肯定是你有你的按钮下方的TasksList TextView的。交换你的layout_above和layout_below值。这适用于我:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView 
     android:id="@+id/task" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/tasks" 
     /> 
    <Button 
     android:id="@+id/btnAddTask" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/AddTasks" 
     android:layout_alignParentBottom="true" 
     /> 
    <TextView 
     android:id="@+id/TasksList" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/tasks" 
     android:layout_above="@id/btnAddTask" 
     android:layout_below="@id/task" 
     /> 
</RelativeLayout> 
1

确实如何解决这个问题。

你必须为按钮,alignParentBottom="true" ...然后为TasksList TextView它被设置在按钮下面...基本上在屏幕之外。

您可以从RelativeLayout删除方向...相对布局没有LinearLayout的方向。

正如你所描述的......我不是100%,你可以用Relative Layout做到这一点。如果你只是想要一件事情在上面,一个在中心,另一个在底部,最好使用单个布局参数centerInParentalignParentTopalignParentBottom

如果你重新考虑LinearLayout(因为我猜你开始),你会坚持垂直方向,然后给他们layout_weight 0,1和0确保高度设置为wrap_content。

<?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"> 
    <TextView 
     android:id="@+id/task" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     /> 
    <TextView 
     android:id="@+id/TasksList" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/tasks" 
     android:layout_weight="1" 
     /> 
    <Button 
     android:id="@+id/btnAddTask" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/AddTasks" 
     android:layout_weight="0" 
     /> 
</LinearLayout> 
1

问题WSA你扭转上述的布局和下面的布局: 尝试

android:layout_below="@id/task" 
android:layout_above="@id/btnAddTask" 

见截图。

这里是完整的布局。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView 
     android:id="@+id/task" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/tasks" 
     /> 
    <Button 
     android:id="@+id/btnAddTask" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/AddTasks" 
     android:layout_alignParentBottom="true" 
     /> 
    <TextView 
     android:id="@+id/TasksList" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/tasks" 
     android:layout_below="@id/task" 
     android:layout_above="@id/btnAddTask" 
     /> 
</RelativeLayout> 

enter image description here

+1

这不会是OP所要求的...应该有3个视图... 2个TextViews和1个按钮。 – Maximus 2011-06-14 17:31:46

+0

我认为与灰色的顶部是活动标题...不是从这种布局的意见... – Maximus 2011-06-14 21:48:50

+0

好吧,你是对的。我没有给予足够的关注。我上面发布了一个更正和一个新的屏幕抓取。 – 2011-06-14 22:04:38