6

如何设置 ListView 的高度。

XML 代码(此代码工作正常,高度固定为 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 代码(此代码不固定宽度和高度)

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

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

提前致谢。

4

4 回答 4

29

试试这个代码..

 LayoutParams lp = (LayoutParams) mListView.getLayoutParams();
 lp.height = 300;
 mListView.setLayoutParams(lp);
于 2012-07-13T12:53:17.163 回答
3
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;
    }

}
于 2015-11-23T14:50:47.813 回答
1

首先删除 LayoutParams 的导入文件,然后为 LayoutParams 使用以下代码。

LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(200, 300);
convertView.setLayoutParams(params);
于 2012-07-13T08:31:53.957 回答
0

试试这个

public void setDynamicHeight(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
    return;
    }
int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
    View listItem = listAdapter.getView(i, null, listView);

    if(listItem != null){
        listItem.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
        totalHeight += listItem.getMeasuredHeight();

    }
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();}
于 2016-07-10T10:55:19.553 回答