{ 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 (the ViewGroup jest zawierający TextView mających długi tekst z wyjątkiem linii paszowej znaków) .getMeasuredHeight zwraca błędną wartość ... że jest mniejsza niż prawdziwa wysokość.ViewGroup {TextView, ...}. GetMeasuredHeight daje niewłaściwy wartość jest mniejsza od rzeczywistej wysokości
jak pozbyć się tego problemu?
tutaj jest kod 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);
}
i tutaj jest list_item_comments.xml:
To mi naprawdę pomogło. Wielkie dzięki! – myforums
@ user711058 Mam ten sam problem, ale ten roztwór nie działa dla mnie – anshul
Ta odpowiedź pomogła mi rozwiązać problem, nad którym pracuję od dłuższego czasu. Mój problem wynikał z braku zrozumienia działania miernika. Mój widok podrzędny użył match_parent dla swojej szerokości, ale mój miary call użył MeasureSpec.makeMeasureSpec (0, MeasureSpec.UNSPECIFIED) dla parametru width. Zmiana parametru width na MeasureSpec.makeMeasureSpec (getWidth(), MeasureSpec.EXACTLY), pozwoliła mi uzyskać prawidłową wysokość z widoku, wywołując metodę view.getMeasuredHeight(). Dziękujemy! – DalvikDroid