我正在Android中使用ScrollView
内部的ListView
。正如我们所知,这会导致高度问题。为此,我正在使用以下代码设置ListView
的大小:
public static void getListViewSize(ListView myListView, Context context) {
ListAdapter myListAdapter = myListView.getAdapter();
if (myListAdapter == null) {
return;
}
int totalHeight = 0;
for (int size = 0; size < myListAdapter.getCount(); size++) {
View listItem = myListAdapter.getView(size, null, myListView);
if (listItem instanceof ViewGroup)
listItem.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
@SuppressWarnings("deprecation")
int screenWidth = display.getWidth();
int listViewWidth = screenWidth - 65;
int widthSpec = MeasureSpec.makeMeasureSpec(listViewWidth,
MeasureSpec.AT_MOST);
listItem.measure(widthSpec, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = totalHeight
+ (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
myListView.setLayoutParams(params);
myListView.requestLayout();
}
Xml :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/lvHomeStream"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:divider="#b5b5b5"
android:dividerHeight="1dp" >
</ListView>
<TextView
android:id="@+id/tvMoreRecords"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginTop="10dp"
android:background="@drawable/list"
android:gravity="center"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:text="@string/viewmore"
android:textColor="@color/blue"
android:textSize="16sp" />
</LinearLayout>
</ScrollView>
如果每个ListView项的高度相同,则可以正常工作。但是,如果每个ListView项的高度不同,则会在ListView结束边界和TextView之间留下额外的大空间。
请帮助我解决这个问题。
listView
放在ScrollView
里面。 - Shayan PourvatanTextView
精确地显示在ListView
的下方,并且没有任何额外的空间。 - Jeeten Parmar