ScrollView 简单应用

ScrollView可实现控件在超出屏幕范围的情况下滚动显示。

用法:在XML文件中将需滚动的控件包含在ScrollView中,当控件超出屏幕范围时可通过滚动查看;ScrollView也提供了一些方法来控制自身的显示情况。

ScrollView中只能包含一个控件,如果用多个,可以放在linearlayout中,

1.布局文件

<?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>
2.自定义listView
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);
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值