在android中运行时设置ListView的高度[英] Set Height of ListView at run time in android

本文是小编为大家收集整理的关于在android中运行时设置ListView的高度的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

如何设置ListView的高度.

XML代码(此代码正常工作,固定高度为200点)

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dip"
    android:orientation="vertical" >
    <ListView
        android:id="@+id/dialog_ListView"
        android:layout_width="wrap_content"
        android:layout_height="200dip"
        android:padding="5dip"
        android:layout_weight="1"
        android:dividerHeight="5dip"
        android:divider="@android:color/transparent" />        
</LinearLayout>

Android代码(此代码不是固定宽度和高度)

convertView = mInflater.inflate( R.layout.dialog_page, null );
convertView.setLayoutParams( new ListView.LayoutParams( 200, 300) );

android代码(我收到错误消息)

convertView = mInflater.inflate( R.layout.lock_file_page, parent );
convertView.setLayoutParams( new ListView.LayoutParams( 200, 300) );

错误消息

java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

如何在运行时修复宽度和高度.

提前感谢.

推荐答案

试试这个代码..

 LayoutParams lp = (LayoutParams) mListView.getLayoutParams();
 lp.height = 300;
 mListView.setLayoutParams(lp);

其他推荐答案

public static boolean setListViewHeightBasedOnItems(ListView listView) {

    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter != null) {

        int numberOfItems = listAdapter.getCount();

        // Get total height of all items.
        int totalItemsHeight = 0;
        for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
            View item = listAdapter.getView(itemPos, null, listView);
            item.measure(0, 0);
            totalItemsHeight += item.getMeasuredHeight();
        }

        // Get total height of all item dividers.
        int totalDividersHeight = listView.getDividerHeight() *
                (numberOfItems - 1);

        // Set list height.
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalItemsHeight + totalDividersHeight;
        listView.setLayoutParams(params);
        listView.requestLayout();

        return true;

    } else {
        return false;
    }

}

其他推荐答案

首先删除layoutparams的导入文件,然后在下面使用layoutparams的代码.

LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(200, 300);
convertView.setLayoutParams(params);

本文地址:https://www.itbaoku.cn/post/102322.html

问题描述

How to set Height of ListView.

XML Code (This code is working fine and fixed height as 200 dip)

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dip"
    android:orientation="vertical" >
    <ListView
        android:id="@+id/dialog_ListView"
        android:layout_width="wrap_content"
        android:layout_height="200dip"
        android:padding="5dip"
        android:layout_weight="1"
        android:dividerHeight="5dip"
        android:divider="@android:color/transparent" />        
</LinearLayout>

Android Code (This code not fixed width and height)

convertView = mInflater.inflate( R.layout.dialog_page, null );
convertView.setLayoutParams( new ListView.LayoutParams( 200, 300) );

Android Code (This code i got Error Message)

convertView = mInflater.inflate( R.layout.lock_file_page, parent );
convertView.setLayoutParams( new ListView.LayoutParams( 200, 300) );

Error Message

java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

How do fix Width and Height at Runtime.

Thanks in advance.

推荐答案

Try This code..

 LayoutParams lp = (LayoutParams) mListView.getLayoutParams();
 lp.height = 300;
 mListView.setLayoutParams(lp);

其他推荐答案

public static boolean setListViewHeightBasedOnItems(ListView listView) {

    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter != null) {

        int numberOfItems = listAdapter.getCount();

        // Get total height of all items.
        int totalItemsHeight = 0;
        for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
            View item = listAdapter.getView(itemPos, null, listView);
            item.measure(0, 0);
            totalItemsHeight += item.getMeasuredHeight();
        }

        // Get total height of all item dividers.
        int totalDividersHeight = listView.getDividerHeight() *
                (numberOfItems - 1);

        // Set list height.
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalItemsHeight + totalDividersHeight;
        listView.setLayoutParams(params);
        listView.requestLayout();

        return true;

    } else {
        return false;
    }

}

其他推荐答案

First Remove import files for LayoutParams and after that Use below code for LayoutParams.

LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(200, 300);
convertView.setLayoutParams(params);