简体   繁体   中英

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. The middle textview covers the whole span in between the button and the top textview. The middle TextView is not being displayed at all. 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. Swap your layout_above and layout_below values. 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.

You can remove orientation from the RelativeLayout ... Relative layout's do not have orientation like LinearLayout's do.

As you're describing... I'm not 100% you can do it with a 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 .

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.

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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