search

Home  >  Q&A  >  body text

android - 在RelativeLayout布局中layout_above不起作用

布局文件代码如下:
layout_above没有起作用,listview把textview覆盖了

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

    <ListView
        android:id="@+id/listView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/listView2"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="20dp"
        android:text="TextView" />

</RelativeLayout>

但是在listview中添加属性android:layout_below="@+id/textView1"就没有问题

PHP中文网PHP中文网2858 days ago1364

reply all(3)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 15:59:08

    Becausewrap_contentListView是不起效的,还是会撑满整个屏幕高度,先写的ListView,屏幕已经无空间了,再加TextView自然是看不见的(而不是覆盖)。还有,严谨的相对id写法应该是android:layout_below="@id/textView1"

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 15:59:08

    You can do 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" >
    
        <ListView
            android:layout_below="@id/textView1"
            android:id="@+id/listView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true" />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_margin="20dp"
            android:text="TextView" />
    
    </RelativeLayout>

    reply
    0
  • PHPz

    PHPz2017-04-17 15:59:08

    The view tree draws views from outside to inside and from top to bottom. The measurement and layout methods of the former will affect the latter, so the dependencies should be placed in the previous view to constrain them.

    reply
    0
  • Cancelreply