简体   繁体   English

相对布局,Textview 未显示

[英]Relative Layout, Textview not showing

I am using this XML to show my one Button at the bottom and one TextView at the top with one TextView right in the middle.我正在使用这个 XML 在底部显示我的一个Button ,在顶部显示一个TextView ,在中间显示一个TextView The middle textview covers the whole span in between the button and the top textview.中间的 textview 覆盖了按钮和顶部 textview 之间的整个跨度。 The middle TextView is not being displayed at all.中间的TextView根本没有显示出来。 What is wrong?怎么了?

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

Your problem is definitely that you have the TasksList TextView below your button.您的问题肯定是您的按钮下方有 TasksList TextView。 Swap your layout_above and layout_below values.交换你的 layout_above 和 layout_below 值。 This works for me:这对我有用:

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

There's definitely something wrong with how you have this laid out.您的布局方式肯定有问题。

You have for the button, alignParentBottom="true" ... then for the TasksList TextView it is set to be below the button... basically off the screen.你有按钮, alignParentBottom="true" ... 然后对于 TasksList TextView它被设置在按钮下方......基本上不在屏幕上。

You can remove orientation from the RelativeLayout ... Relative layout's do not have orientation like LinearLayout's do.您可以从RelativeLayout中删除方向......相对布局没有像 LinearLayout 那样的方向。

As you're describing... I'm not 100% you can do it with a Relative Layout .正如你所描述的......我不是 100% 你可以用Relative Layout来做到这一点。 If you simply wanted one thing on top, one centered and the other on bottom, it would be best to use a single layout parameter for each one, centerInParent , alignParentTop and alignParentBottom .如果您只是想要一件事在顶部,一件事居中,另一件事在底部,最好为每件事使用一个布局参数centerInParentalignParentTopalignParentBottom

If you reconsider LinearLayout (as I'm guessing you started with) you'd stick with a Vertical Orientation, then give them layout_weight of 0, 1, and 0 making sure the height was set to wrap_content.如果你重新考虑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>

The problem wsa that you reversed your layout above and the layout below: try问题是您颠倒了上面的布局和下面的布局:尝试

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

See screenshot.见截图。

And here is the complete layout.这是完整的布局。

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

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM