如何解决在scrollview中的viewpager高度自适应的

本文详细介绍了如何创建一个能够自动适应高度的ViewPager组件,包括其核心原理、关键代码解析及实际应用案例。
时间:2016年3月18日16:47:56


   
  1. /**
  2. * 自动适应高度的ViewPager
  3. * @author
  4. *
  5. */
  6. public class CustomViewPager extends ViewPager {
  7. public CustomViewPager(Context context) {
  8. super(context);
  9. }
  10. public CustomViewPager(Context context, AttributeSet attrs) {
  11. super(context, attrs);
  12. }
  13. @Override
  14. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  15. int height = 0;
  16. for (int i = 0; i < getChildCount(); i++) {
  17. View child = getChildAt(i);
  18. child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
  19. int h = child.getMeasuredHeight();
  20. if (h > height)
  21. height = h;
  22. }
  23. heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
  24. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  25. }
  26. }





转载于:https://www.cnblogs.com/yuzhongzheng/p/5412577.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值