[英]Custom View Width
I've created a class for a custom view, and I overrode the onMeasure method like I show next. 我已经为自定义视图创建了一个类,并且覆盖了如下所示的onMeasure方法。 But when i call the getter for width and height, their always returns 0. Why does that happen?
但是,当我为宽度和高度调用吸气剂时,它们总是返回0。为什么会这样?
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
this.setMeasuredDimension(parentWidth, parentHeight);
}
getWidth()和getHeight()值仅在第一次完整布局传递(OnMeasure-> OnLayout)之后才可用,因此,如果您确实需要在使用getMeasuredWidth()和getMeasuredHeight()之间访问这些值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.