首页 > 技术文章 > 如何解决在scrollview中的viewpager高度自适应的

yuzhongzheng 2016-04-20 14:31 原文

时间: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. }



来自为知笔记(Wiz)


推荐阅读