有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么我不能在相对布局中的另一个视图上方看到文本视图?

我有一个xml:

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

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

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



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




</RelativeLayout>

但我无法在eclipse图形预览器中看到pointsText

我错过了什么


共 (3) 个答案

  1. # 1 楼答案

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

  2. # 2 楼答案

    你有

    android:layout_above="@+id/offers_list"
    

    对于你的TextView,但是你没有任何关于ListView的定位。由于默认情况下RelativeLayout会将其孩子放在左上方,因此ListView将位于顶部,并且在其上方没有任何地方可以放置TextView

    尝试从TextView中删除该属性并添加

    android:layout_below="@id/pointsText"
    

    在你的ListView

    我刚刚测试了这个,它确实有效。我可以看到列表上方的pointsTextTextView。因此,您需要对错误TextView做一些不同的处理,或者当您想要更改它的visibility时,您很可能会遇到同样的问题

    有点离题

    RelativeLayout没有orientation属性,所以这对你没有任何好处,但也不会对你造成任何实际伤害

  3. # 3 楼答案

    使用相对布局时,布局元素彼此重叠。为了避免这种情况,您应该相对其他布局放置元素。让你的代码像这样

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

    现在,您将能够看到您的所有布局