zoukankan      html  css  js  c++  java
  • Android ViewPager无法使用wrap_content属性自适应高度

    使用ViewPager的时候发现一个问题,当设置ViewPager控件的height属性为wrap_content时,控件高度一直是0,无法正常显示,在网上找到了解决办法,重写ViewPager的onMesure()方法.

    public class WrapContentHeightViewPager extends ViewPager {
        /**
         * Constructor
         *
         * @param context
         *            the context
         */
        public WrapContentHeightViewPager(Context context) {
            super(context);
        }
        /**
         * Constructor
         *
         * @param context
         *            the context
         * @param attrs
         *            the attribute set
         */
        public WrapContentHeightViewPager(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            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();
                if (h > height)
                    height = h;
            }
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
                    MeasureSpec.EXACTLY);
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }  
    
  • 相关阅读:
    在Matlab2018b中配置MinGW-w64 C/C++ 编译器
    电脑忽然黑屏
    Linux中drwxr-xr-x.的意思和权限
    tensorflow 和cuda对应关系
    apt-get update 升级错误
    修改模型参数名
    tensor转化为ndarray
    Ubuntu GitLab仓库服务器搭建
    友元
    常函数 常对象
  • 原文地址:https://www.cnblogs.com/fortitude/p/5478869.html
Copyright © 2011-2022 走看看