继承ViewGroup后的子类如何重写onMeasure方法
2012-07-13 00:19
441 查看
1.首先贴上我试验成功的代码
解释一下:
首先判断params.width的值是多少,有三种情况。
如果是大于零的话,及传递的就是一个具体的值,那么,构造MeasupreSpec的时候可以直接用EXACTLY。
如果为-1的话,就是MatchParent的情况,那么,获得父View的宽度,再用EXACTLY来构造MeasureSpec。
如果为-2的话,就是wrapContent的情况,那么,构造MeasureSpec的话直接用一个负数就可以了。
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int measureWidth = MeasureSpec.getSize(widthMeasureSpec); int measureHeigth = MeasureSpec.getSize(heightMeasureSpec); setMeasuredDimension(measureWidth, measureHeigth); // TODO Auto-generated method stub for(int i= 0;i<getChildCount();i++){ View v = getChildAt(i); Log.v(TAG, "measureWidth is " +v.getMeasuredWidth() + "measureHeight is " +v.getMeasuredHeight()); int widthSpec = 0; int heightSpec = 0; LayoutParams params = v.getLayoutParams(); if(params.width > 0){ widthSpec = MeasureSpec.makeMeasureSpec(params.width, MeasureSpec.EXACTLY); }else if (params.width == -1) { widthSpec = MeasureSpec.makeMeasureSpec(measureWidth, MeasureSpec.EXACTLY); } else if (params.width == -2) { widthSpec = MeasureSpec.makeMeasureSpec(measureWidth, MeasureSpec.AT_MOST); } if(params.height > 0){ heightSpec = MeasureSpec.makeMeasureSpec(params.height, MeasureSpec.EXACTLY); }else if (params.height == -1) { heightSpec = MeasureSpec.makeMeasureSpec(measureHeigth, MeasureSpec.EXACTLY); } else if (params.height == -2) { heightSpec = MeasureSpec.makeMeasureSpec(measureWidth, MeasureSpec.AT_MOST); } v.measure(widthSpec, heightSpec); } }
解释一下:
首先判断params.width的值是多少,有三种情况。
如果是大于零的话,及传递的就是一个具体的值,那么,构造MeasupreSpec的时候可以直接用EXACTLY。
如果为-1的话,就是MatchParent的情况,那么,获得父View的宽度,再用EXACTLY来构造MeasureSpec。
如果为-2的话,就是wrapContent的情况,那么,构造MeasureSpec的话直接用一个负数就可以了。
相关文章推荐
- 继承ViewGroup后的子类如何重写onMeasure方法
- 解析:继承ViewGroup后的子类如何重写onMeasure方法
- 继承ViewGroup:重写onMeasure方法和onLayout方法
- 继承ViewGroup:重写onMeasure方法和onLayout方法
- Android 继承ViewGroup必须重写onMeasure方法和onLayout方法
- 继承ViewGroup:重写onMeasure方法和onLayout方法
- 继承ViewGroup:重写onMeasure方法和onLayout方法
- 继承ViewGroup:重写onMeasure方法和onLayout方法 及 方法执行过程
- 继承ViewGroup重写onMeasure方法的详解
- 继承ViewGroup:重写onMeasure方法和onLayout方法
- 继承ViewGroup重写onMeasure方法的详解
- Android 重写ViewGroup 分析onMeasure()和onLayout()方法
- 1.为什么需要继承?如何继承?继承到什么? 2.方法的重载与重写的区别 3.类型的自动转换和强制转换 4.子类特有的方法的调用规则 5.子类重写的方法的调用规则 6.多态的理解 7.面向对象的三大特征
- 在继承SurfaceView的类中如何重写onDraw()方法使其有效
- Android自定义控件:imageview重写onMeasure方法实现图片按指定比例显示,拉伸永不变形,解决屏幕适配问题
- JAVA中子类是否可以可以继承、重写父类的静态方法--总结
- Java子类继承(二):隐藏成员变量和方法重写的理解
- C#下 子类继承父类的虚方法后 使用override重写父类方法 和 不使用override重写有什么区别?
- 子类继承父类(重写父类的静态方法,子类方法必须也是静态的)
- JS中子类中如何调用父类的同名方法(继承时候,父类的同名方法被覆盖了)