What triggers a View's measure() to be called

前端 未结 2 1219
独厮守ぢ
独厮守ぢ 2021-02-05 00:13

In my application I have an infinite loop on one of my View\'s onMeasure() overrides. Debugging the source code starting from a break point in my onMeasure, I am able to trace

2条回答
  •  温柔的废话
    2021-02-05 00:32

    In order to trigger a measure pass for a custom View you must call the requestLayout() method. For example, if you are implementing a custom view that extends View and it will behave like a TextView, you could write a setText method like this:

    /**
     * Sets the string value of the view.
     * @param text the string to write
     */
    public void setText(String text) {
        this.text = text;
    
                //calculates the new text width
        textWidth = mTextPaint.measureText(text);
    
        //force re-calculating the layout dimension and the redraw of the view
        requestLayout();
        invalidate();
    }
    

提交回复
热议问题