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);
        }
    }  
    
  • 相关阅读:
    HDFS详解(3)——HDFS文件结构
    HDFS详解(1)
    MapReduce工作机制
    Hadoop体系结构
    Hadoop 项目及结构
    (转)Hadoop生态系统
    Hadoop配置参数
    HDFS详解(2)——HDFS中的读写数据流
    Yarn(MapReduce V2)
    与或非实习day02
  • 原文地址:https://www.cnblogs.com/fortitude/p/5478869.html
Copyright © 2011-2022 走看看