Search code examples
androidandroid-layoutandroid-relativelayout

Views under ListView are not displayed in Relative layout


  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some Text1"
        android:textSize="22dp"
        android:textStyle="bold"
        android:textColor="@color/black"
        android:id="@+id/header"
        android:layout_centerHorizontal="true"/>
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/header"
        android:id="@+id/list"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some Text2"
        android:layout_below="@id/list"
        android:id="@+id/text"
        />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/image"
        android:layout_below="@id/text"
        />
</RelativeLayout>

Above is my layout , listview is scrollable but when i scroll to the bottom of the listview I expect the TextView and ImageView to be rendered after the listview.However they are not shown at all.List view covers the whole page (besides the header at the top) What could I do to render the textview and the imageview below the listview ?


Solution

  • There are two ways to do it: First LinearLayout using weight 1 to Listview:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Some Text1"
            android:textSize="22dp"
            android:textStyle="bold"
            android:textColor="@color/black"
            android:id="@+id/header"
            android:layout_centerHorizontal="true"/>
        <ListView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:id="@+id/list"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Some Text2"
            android:id="@+id/text"
            />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/image"
            />
    </LinearLayout>
    

    other 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">
    
        <TextView
            android:id="@+id/header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="Some Text1"
            android:textColor="@color/black"
            android:textSize="22dp"
            android:textStyle="bold" />
    
        <ListView
            android:id="@+id/list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/text"
            android:layout_below="@id/header" />
    
        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/image"
            android:text="Some Text2" />
    
        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" />
    </RelativeLayout>
    

    Hope this helps.