[英]why cannot i see a textView above another view in relative 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"
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>
但是,我在日食圖形預覽器中看不到pointsText。
我想念什么?
你有
android:layout_above="@+id/offers_list"
為您的TextView
但您對ListView
沒有任何定位。 由於默認情況下RelativeLayout
會將其子代放置在左上角,因此ListView
會在頂部,並且在其上方沒有位置可以放置TextView
。
嘗試從TextView
刪除該屬性並添加
android:layout_below="@id/pointsText"
在您的ListView
。
我剛剛測試了它,它確實起作用。 我可以在列表上方看到pointsText TextView
。 因此,您將希望對錯誤TextView
做一些不同的事情,或者當您想要更改其visibility
時,很可能會遇到同樣的問題。
有點題外話
RelativeLayout
沒有orientation
屬性,因此不會給您帶來任何好處,但也不會給您帶來任何實質性的傷害。
當您使用相對布局時,布局元素會相互放置。 為了防止這種情況,您應該相對於其他布局放置元素。 使您的代碼像這樣。
<?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和添加片段的布局
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.