写了一个界面,外面是滚动条,然后里面嵌套了ListView,然后就超出了当前界面的总高度,但是在这个ListView后面我还加了一个ImageView,运行出来的时候,ListView只显示了一行,以前用的方法是直接不停地猜测ListView的高度,去给它写死,但是用自定义ListView更好一点:
测试的XML文件的布局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试" />
<!--这儿的ListView写成你自己继承的那个ListView,包名+类名 -->
<com.example.oneandroidstudy03.demo.myListView
android:id="@+id/list_scroll_main_demo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadingEdge="none" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/tiaozhan" />
</LinearLayout>
</ScrollView>
自定义的ListView,3个构造方法和一个onMeasure()方法;
public class myListView extends ListView {
public myListView(Context context) {
super(context);
}
public myListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public myListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
//----------高度就是你计算出来的那个高度-_-expandSpec-----
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
主Activity里面,就是实例化ListView和设置适配器就可以了:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scroll_activity_layout);
mListView = (ListView) findViewById(R.id.list_scroll_main_demo);
SimpleAdapter adapter = new SimpleAdapter(this, getList(),
R.layout.list_demo, new String[] { "TEXT" },
new int[] { R.id.demo_text });
mListView.setAdapter(adapter);
}
没用自定义之前的结果:
改成自定义的ListView后: