我有列表視圖根據評論長度有不同高度的每行評論列表。我想以編程方式給列表視圖添加高度。我試圖讓每一行的高度,但每次我得到每行相同的高度......但根據他們的內容,每行有不同的大小。我已使用以下代碼:view.getMeasuredHeight()總是返回相同的高度android
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, View.MeasureSpec.UNSPECIFIED);
totalItemsHeight += item.getMeasuredHeight();
Log.e("HEIGHT", "" + 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();
}
你有一個XML格式的固定高度設爲您的列表項? –
檢查你是否在xml中給出了固定的高度? –