【问题标题】:Fixing android listview size to a particular length将android listview大小固定为特定长度
【发布时间】:2012-12-29 07:28:24
【问题描述】:

我创建了一个布局文件。里面还有其他布局。 其中还有一个列表视图。我给它固定高度。 它在我的屏幕上工作正常,但是当我们将它用于其他屏幕尺寸时 它没有完整显示。它如何适用于所有尺寸的设备。 代码如下。

<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:weight ,以便您的列表视图可以根据您的屏幕尺寸调整大小。

标签: android listview layout android-relativelayout


【解决方案1】:

您必须根据设备屏幕大小预先确定行数,然后计算每行的高度。您不应明确给出 android:layout_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;
    }

【讨论】:

    猜你喜欢
    • 2012-08-14
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 2016-01-15
    • 1970-01-01
    • 1970-01-01
    • 2012-01-24
    • 2013-08-12
    相关资源
    最近更新 更多