Android开发遇到的问题:不给include设置width、height,导致ListView GridView内容无法显示

我的目的是做一个带有TextView的ListView列表页面。

以下是这个页面的xml:

<?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" >
    
    <include 
        layout="@layout/head"/>
    
    <GridView
        android:id="@+id/gv_home" 
        android:numColumns="3"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
        
</LinearLayout>

以下是这个页面的head.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" >
    
    <TextView 
        android:id="@+id/tv_head"
        android:text="@string/loading"
        android:textSize="22sp"
        android:background="@color/bg_color"
        android:textColor="#000000"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="55dip"/>

</RelativeLayout>

可以看到我设置了head.xml宽高都是match_parent,而不给Include设定宽高,那么相当于给Include设定了match_parent,这样导致下面的ListView被挤到屏幕下面了。

导致ListView  GridView的内容不显示。

解决办法:

1. 给Include设定高度为wrap_content

2. 给head.xml的RelativeLayout设定高度为wrap_content

原文地址:https://www.cnblogs.com/hoosway/p/4925963.html