Android ViewPage高度wrap_content不起作用的问题

今天做一个欢迎页面,发现ViewPage始终占据父容器LinearLayout的剩余高度。

网上的解决方案都是重写ViewPage的onMeasure()

public class WrapContentHeightViewPager extends ViewPager {

    public WrapContentHeightViewPager(Context context) {
        super(context);
    }
    
    public WrapContentHeightViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

       //以子page高度的最高值为ViewPage的高度
        int height = 0;
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            int h = child.getMeasuredHeight() + this.getPaddingTop() + this.getPaddingBottom();
            if (h > height) height = h;
        }

        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

高度要记得加上paddingTop和paddingBottom,  网上的解决方案里是没有的。这样会有问题。

如果你正好用到了paddingTop/paddingBottom, 那么ViewPage会被截掉一些,被截掉的高度正好是paddingTop+paddingBottom的值。

 

这儿再重点说一下MeasureSpec.UNSPECIFIED

有一些父容器是那种不限制子view高度的控件。比如ScrollView, ViewPage等。这类控件的内容高度是无限的。所以高度用wrap_content是没有效果的。要使wrap_content有效果,必须重写onMeasure(),  然后在测量子View的时候, 把MeasureSpec.UNSPECIFIED传给子View,  告诉子View你们的内容高度是多少就回报给我多少。然后父控件就拿到子View们的高度后,就可以设置父控件的高度了。 这样一来wrap_content就有用了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值