Java 为什么在相对布局中无法在另一个视图上方看到文本视图?
我有以下xml:Java 为什么在相对布局中无法在另一个视图上方看到文本视图?,java,android,android-layout,layout,Java,Android,Android Layout,Layout,我有以下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"
<?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>
但是我在eclipse图形预览器中看不到pointsText
我错过了什么?你错过了什么
android:layout_above="@+id/offers_list"
用于TextView,但您没有任何ListView定位。由于RelativeLayouts默认将其子项放在左上角,因此ListView将位于顶部,并且在其上方没有任何位置可以放置TextView
尝试从TextView中删除该属性并添加
在您的ListView上
我刚刚测试了这个,它确实有效。我可以看到列表上方的pointsText文本视图。因此,您将希望对错误文本视图执行一些不同的操作,或者当您想要更改其可见性时,您很可能会遇到相同的问题
有点离题
RelativeLayout没有方向属性,因此对您没有任何好处,但也不会对您造成任何实际伤害。使用相对布局时,布局元素彼此重叠。为了避免这种情况,您应该相对于其他布局放置图元。让你的代码像这样
<?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>
现在,您将能够看到您的所有布局尝试以下方法:
<?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和添加片段的布局
<?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>