【发布时间】:2019-06-17 15:23:20
【问题描述】:
我有一个设置了特定字体和字体大小的 TextView。我想确定多行文本是否适合此 TextView(TextView 的宽度和高度都设置为 match_parent)。 有什么合理的方法吗?
谢谢
【问题讨论】:
-
是的。您可以使用我发布的解决方案here。
我有一个设置了特定字体和字体大小的 TextView。我想确定多行文本是否适合此 TextView(TextView 的宽度和高度都设置为 match_parent)。 有什么合理的方法吗?
谢谢
【问题讨论】:
您可以使用这种方法测量文本的边界:
String yourText = "...";
Rect bound = new Rect();
textView.getPaint().getTextBounds(yourText , 0, yourText.length(), bounds);
然后你就可以得到文本视图的宽度和高度了:
bounds.width() or bounds.height();
【讨论】:
如果你把你的 TextView 放在一个 ScrollView 里面,你会知道它会 100% 适合 :)
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Ty Juan" />
</ScrollView>
【讨论】: