【问题标题】:How to adjust height and width of TextView according to EditText properties?如何根据EditText属性调整TextView的高宽?
【发布时间】:2018-05-10 20:42:02
【问题描述】:

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

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

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

【问题讨论】:

标签: android android-edittext textview


【解决方案1】:

您可以将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 获得所需的宽度和高度。

【讨论】:

  • 谢谢 看起来很好.. 会试试
  • 我可以通过设置 bounds 获得 textSize 吗?什么文本大小将适合给定字体系列和文本的边界
  • 我不这么认为,你可以尝试不同的文本大小并检查边界
猜你喜欢
  • 2019-01-06
  • 2013-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-22
  • 2014-08-24
  • 2022-01-26
相关资源
最近更新 更多