简体   繁体   中英

Android Listview not shown in relative layout

I have a relative layout to show a menu with some buttons and edittext. This is displayed fine. When I add a listview to a parent relative layout (to display the listview below the menu layout), but the listview is not shown. My XML code is inserted below.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:id="@+id/listactivity"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    <RelativeLayout
        android:id="@+id/menu"
        android:layout_width="match_parent"
        android:layout_height="75dp"
        android:background="#ff0000"
        android:orientation="vertical"
        android:focusableInTouchMode="true" 
        android:layout_gravity="top" >

        <Button
            android:id="@+id/cities"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cities"
            android:textStyle="bold"
            android:onClick="showPopup" />

    <Button
        android:id="@+id/addcity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="30dp"
        android:text="+"
        android:textStyle="bold"
        android:layout_toRightOf="@+id/cities" />

    <Button
        android:id="@+id/days"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Days"
        android:textStyle="bold"
        android:layout_toRightOf="@+id/addcity" />

    <Button
        android:id="@+id/addday"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/days"
        android:minWidth="30dp"
        android:text="+"
        android:textStyle="bold" />

    <Button
        android:id="@+id/Days"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:layout_toRightOf="@+id/addday"
        android:textStyle="bold"
        android:onClick="showListview" />

    <EditText
        android:id="@+id/myEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_below="@+id/cities"
        android:ems="10" >
    </EditText>

</RelativeLayout>

    <ListView
        android:id="@+id/listview"
        android:background="#fff000"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:layout_below="@+id/menu" />

</RelativeLayout>

It might be because your list view has a height set of wrap_content you should avoid using this for lists. Try setting it to a specific height and see if that helps

Here is a link to a good answer about this https://stackoverflow.com/a/4271930/1417483

Because the ListView's height is set to wrap_content, it will have a height of 0dp until you add something to it.

Make sure you initialise the ListView and add some dummy data to it. The other option is to set the ListView height to stretch to the bottom of the screen like this:

    <ListView
        android:id="@+id/listview"
        android:background="#fff000"
        android:layout_width="150dp"
        android:layout_height="0dp"
        android:visibility="visible"
        android:layout_below="@+id/menu"
        android:layout_alignParentBottom="true"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM