Get the size of a text in TextView

前端 未结 4 679
自闭症患者
自闭症患者 2020-12-05 01:53

I have a problem placing a textView at specified center\'s x and y coordinates. Firstly, I tried to set the text in the textView, and to move the view with the width and the

4条回答
  •  臣服心动
    2020-12-05 02:21

    For some reason the solution of Midverse Engineer does not give me always correct results (at least on some Android versions). The solution of Sherif elKhatib works, but has the side effect of changing MeasuredWidth and Height. This could lead to incorrect positioning of the textView.

    My solution:

    width = textView.getPaint().measureText(text);
    metrics = textView.getPaint().getFontMetrics();
    height = metrics.bottom - metrics.top;
    

提交回复
热议问题