2012-07-13 74 views
3

如何設置ListView的高度。在運行時設置ListView的高度android

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 

如何在運行時修復寬度和高度。

在此先感謝。

+0

請參閱我的答案。 – 2012-07-13 12:23:09

回答

22

試試這個代碼..

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

'LinearLayout.LayoutParams lp =(LayoutParams)mListview.getLayoutParams();'添加此工作正常。 – Sekar 2012-07-19 11:11:10

+0

謝謝。此代碼工作正常.. – Sekar 2012-07-19 11:11:28

+0

對不起,我不知道ios。 – 2016-05-24 05:58:08

1

首先刪除LayoutParams的導入文件,之後使用LayoutParams的以下代碼。

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

此代碼無法正常工作。我得到了同樣的錯誤消息'java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams不能轉換爲android.widget.AbsListView $ LayoutParams' – Sekar 2012-07-13 13:18:06

+0

@ user1474868請將您的完整代碼與導入語句,所以我會給你適當的解決方案。 – 2012-07-14 04:39:31

+0

該代碼工作,謝謝:) – 2016-10-09 00:35:35

1
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; 
    } 

} 
+0

給出一個簡短的描述,你做了什麼,爲什麼隨着你的代碼會很好。 – 2015-11-23 15:08:04

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();} 
相關問題