{ January 14, 2011... I have given up to use setListViewHeightBasedOnChildren(ListView listView},
instead, I don't put my listview in a scrollview, and then just put other contents
into a listview by using ListView.addHeaderView() and ListView.addFooterView().
http://dewr.egloos.com/5467045 }
のViewGroup(のViewGroupは改行文字以外長いテキストを有するTextViews含有される)...その実際の高さよりも小さくなっている与えます実際の高さよりも小さい。のViewGroup {のTextView、...}。getMeasuredHeightは間違った値が間違った値を返す.getMeasuredHeight
この問題を解決するにはどうすればよいですか?ここ
は、Javaコードです:
/*
I have to set my listview's height by myself. because
if a listview is in a scrollview then that will be
as short as the listview's just one item.
*/
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int count = listAdapter.getCount();
for (int i = 0; i < count; i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(View.MeasureSpec.AT_MOST, View.MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
、ここではlist_item_comments.xmlです:
これは本当に私を助けました。どうもありがとう! – myforums
@ user711058私は同じ問題を抱えていますが、このソリューションは私のために働いていません – anshul
この回答は私がしばらくの間取り組んできた問題を解決するのを助けました。私の問題は、尺度がどのように働いたかの理解の欠如から来た。 私の子ビューではwidthにmatch_parentを使用しましたが、私のメジャーコールではwidthパラメータにMeasureSpec.makeMeasureSpec(0、MeasureSpec.UNSPECIFIED)を使用しました。 widthパラメータをMeasureSpec.makeMeasureSpec(getWidth()、MeasureSpec.EXACTLY)に変更すると、view.getMeasuredHeight()を呼び出してビューから正しい高さを取得できました。 ありがとうございます! – DalvikDroid