ScrollView可实现控件在超出屏幕范围的情况下滚动显示。
用法:在XML文件中将需滚动的控件包含在ScrollView中,当控件超出屏幕范围时可通过滚动查看;ScrollView也提供了一些方法来控制自身的显示情况。
ScrollView中只能包含一个控件,如果用多个,可以放在linearlayout中,
1.布局文件
2.自定义listView<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.bwie.day04_listview_scrollview.MainActivity"> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <com.bwie.day04_listview_scrollview.Mylistview android:id="@+id/lv" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </ScrollView> </LinearLayout>
public class Mylistview extends ListView { public Mylistview(Context context) { this(context,null); } public Mylistview(Context context, AttributeSet attrs) { this(context, attrs,0); } public Mylistview(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //重新设置 //MeasureSpec 两个参数 1.size 2.mode int heightMeasure = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, heightMeasure); } }