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);
        }
    }  
    
  • 相关阅读:
    mysql内置函数
    phpmyadmin 安装
    java 命令行JDBC连接Mysql
    数据库工具
    java JDBC
    mysql 各种关系代数的使用
    恢复误删的DB table数据
    eclipse php pdt插件安装
    mysql通配符使用
    关系代数和sql语句对应关系
  • 原文地址:https://www.cnblogs.com/fortitude/p/5478869.html
Copyright © 2011-2022 走看看