简体   繁体   English

Android TextView - 确定多行文本是否适合TextView

[英]Android TextView - determine if multiline text will fit into the TextView

I have a TextView with specific font and font size set. 我有一个TextView具有特定的字体和字体大小设置。 I want to determine if a multiline text will fit into this TextView (the TextView is set to match_parent for both width and height). 我想确定多行文本是否适合此TextView(TextView的宽度和高度都设置为match_parent)。 Is there any reasonable way of doinf it? 有没有合理的方式来做它?

Thanks 谢谢

You can measure the bounds of your text using this approach: 您可以使用以下方法测量文本的边界:

String yourText = "...";
Rect bound = new Rect();
textView.getPaint().getTextBounds(yourText , 0, yourText.length(), bounds);

Then you can get width and height of text for the giving textview: 然后,您可以获取给出textview的文本的宽度和高度:

bounds.width() or bounds.height();

If you put your TextView inside a ScrollView, you will know it will fit 100% :) 如果您将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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM