简体   繁体   中英

why cannot i see a textView above another view in relative layout?

I have this xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="8dp"
    android:paddingRight="8dp" >

        <TextView
        android:id="@+id/errorMsg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_above="@+id/pointsText"
        android:visibility="gone"/>

                <TextView
        android:id="@+id/pointsText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_above="@+id/offers_list"
        android:text="you have 100 points"
        android:visibility="visible"/>



    <ListView
        android:id="@+id/offers_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="false" 
        android:dividerHeight="0dp"
        android:divider="#00000000"/>




</RelativeLayout>

Yet I cannot see pointsText in my eclipse graphical previewer.

What am I missing?

You have

android:layout_above="@+id/offers_list"

for your TextView but you don't have any positioning for your ListView . Since RelativeLayout s place their children at the top-left by default, your ListView would be at the top and there would be nowhere above it to place your TextView .

Try removing that property from your TextView and add

android:layout_below="@id/pointsText"

on your ListView .

I just tested this and it does work. I can see the pointsText TextView above the list. So, you will want to do something different with your error TextView or you will quite possibly have the same issue with that when you want to change its visibility .

A bit off-topic

RelativeLayout doesn't have an orientation property so that doesn't do you any good but doesn't do you any real harm either.

When you use relative layouts, layout elements lay upon each other. To prevent that you should place element Relatively to other layouts. make your code like this.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="8dp"
    android:paddingRight="8dp" >

       <TextView
        android:id="@+id/errorMsg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_above="@+id/pointsText"
        android:visibility="gone"/>

       <TextView
        android:id="@+id/pointsText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_above="@+id/offers_list"
        android:text="you have 100 points"
        android:visibility="visible"
        android:layout_below="@+id/errorMsg"/>



    <ListView
        android:id="@+id/offers_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="false" 
        android:dividerHeight="0dp"
        android:divider="#00000000"
        android:layout_below="@+id/pointsText"/>
</RelativeLayout>

Now you will be able to see your all layouts

try this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp" >

<LinearLayout 
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:orientation="vertical" >

    <TextView
        android:id="@+id/errorMsg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:visibility="gone"/>

    <TextView
        android:id="@+id/pointsText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:text="you have 100 points"
        android:visibility="visible"/>

</LinearLayout>

<ListView
    android:id="@+id/offers_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/linearLayout1"
    android:divider="#00000000"
    android:dividerHeight="0dp"
    android:drawSelectorOnTop="false" />

</RelativeLayout>

如果这是活动中的片段,则可能要检查例如嵌套的AppBarLayout和添加片段的布局

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