<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}"
android:id="@+id/ddd" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<com.example.test_listview.MyListView
android:id="@+id/mylist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
></com.example.test_listview.MyListView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你猜猜"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你猜猜"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你猜猜"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你猜猜"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你猜猜"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你猜猜"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你猜猜"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你猜猜"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你猜猜"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你猜猜"/>
</LinearLayout>
</ScrollView>
listview = (ListView) findViewById(R.id.mylist);
调用 setListViewHeight(listview);
private void setListViewHeight(ListView listView) {
ListAdapter listAdapter =listView.getAdapter();
int count = listAdapter.getCount();
int totalHeight = 0;
for(int i=0;i<count;i++){
View itemView =listAdapter.getView(i, null, listView);
itemView.measure(0, 0);
totalHeight+=itemView.getMeasuredHeight();
totalHeight+=listView.getDividerHeight();
}
totalHeight-=listView.getDividerHeight();
ViewGroup.LayoutParams layoutParams = listView.getLayoutParams();
layoutParams.height=totalHeight;
}
自定义ListView计算高度
<com.example.test_listview.MyListView
android:id="@+id/mylist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
></com.example.test_listview.MyListView>
package com.example.test_listview;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ListView;
public class MyListView extends ListView{
public MyListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyListView(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
heightMeasureSpec=MeasureSpec.makeMeasureSpec(300,MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}