将android listview大小固定为特定长度

将android listview大小固定为特定长度,android,listview,layout,android-relativelayout,Android,Listview,Layout,Android Relativelayout,我已经创建了一个布局文件。其中还包括其他布局。 其中还有一个列表视图。我给它定了高度。 它在我的屏幕上运行良好,但当我们将其用于其他屏幕大小时 它没有完全显示出来。它如何适用于所有尺寸的设备。 代码如下所示 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" andr

我已经创建了一个布局文件。其中还包括其他布局。 其中还有一个列表视图。我给它定了高度。 它在我的屏幕上运行良好,但当我们将其用于其他屏幕大小时 它没有完全显示出来。它如何适用于所有尺寸的设备。 代码如下所示

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
     android:background="@drawable/bg" >

    <include
        android:id="@+id/mainScreenHeader"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        layout="@layout/main_screen_header" />


    <include
        android:id="@+id/mainScreenListHeader"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/mainScreenHeader"
        layout="@layout/main_screen_list_header" >
    </include>

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="270dp"
        android:layout_below="@+id/mainScreenListHeader"
        android:cacheColorHint="#00000000"
        android:drawSelectorOnTop="false" />

     <include
        android:id="@+id/mainScreenFilterClient"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/list"
        android:layout_marginTop="10dp"
        layout="@layout/main_screen_filter_client" >
    </include>

    <include
        android:id="@+id/footer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/mainScreenFilterClient"
        android:layout_marginTop="10dp"
        layout="@layout/footer" >
    </include>
    </RelativeLayout>

您必须根据设备屏幕大小预先确定行数,然后计算每行的高度。您不应明确给出android:layout\u height=“270dp”。 您可以获得屏幕的高度,要确定列表视图中的行数,请参考以下内容:

    private int retrieveDimensions() {
        int noOfRows= 0;
        int screenSize = getResources().getConfiguration().screenLayout &
                Configuration.SCREENLAYOUT_SIZE_MASK;

        switch(screenSize) {
        case Configuration.SCREENLAYOUT_SIZE_LARGE:
        case Configuration.SCREENLAYOUT_SIZE_XLARGE:
            noOfRows= 5;
            break;
        case Configuration.SCREENLAYOUT_SIZE_NORMAL:
            noOfRows= 4;
            break;
        case Configuration.SCREENLAYOUT_SIZE_SMALL:
            noOfRows= 3;
            break;
    }

尝试使用android:weight,以便可以根据屏幕尺寸调整listview的大小。