简体   繁体   English

为什么我不能以相对布局在另一个视图上方看到textView?

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

I have this xml: 我有这个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. 但是,我在日食图形预览器中看不到pointsText。

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 . 为您的TextView但您对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 . 由于默认情况下RelativeLayout会将其子代放置在左上角,因此ListView会在顶部,并且在其上方没有位置可以放置TextView

Try removing that property from your TextView and add 尝试从TextView删除该属性并添加

android:layout_below="@id/pointsText"

on your ListView . 在您的ListView

I just tested this and it does work. 我刚刚测试了它,它确实起作用。 I can see the pointsText TextView above the list. 我可以在列表上方看到pointsText TextView 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 . 因此,您将希望对错误TextView做一些不同的事情,或者当您想要更改其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. RelativeLayout没有orientation属性,因此不会给您带来任何好处,但也不会给您带来任何实质性的伤害。

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和添加片段的布局

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

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