简体   繁体   中英

Custom View Width

I've created a class for a custom view, and I overrode the onMeasure method like I show next. But when i call the getter for width and height, their always returns 0. Why does that happen?

@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()之间访问这些值

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM