如何根据EditText属性调整TextView的高度和宽度?

How to adjust height and width of TextView according to EditText properties?

我正在创建一个图像编辑器。要在图像上写文字,我们需要文本自动调整大小功能。 同样,我想根据

计算 editText 的高度
  1. 字符数
  2. editText 的宽度
  3. 字体大小

我认为它也会随着 font-family 和 font-weight 的变化而变化,如果是这样的话,在计算 editText 的高度时如何考虑所有这些。

您只需设置 editText 所需的字体,然后调用:

Rect bounds = new Rect(0, 0, 0, 0);
String textToMeasure = "SOME TEXT"; 
Paint textPaint = editText.getPaint();
textPaint.getTextBounds(textToMeasure, 0, textToMeasure.length(), bounds);

然后您将从 bounds 获得所需的宽度和高度。