【问题标题】:Android TextView - determine if multiline text will fit into the TextViewAndroid TextView - 确定多行文本是否适合 TextView
【发布时间】:2019-06-17 15:23:20
【问题描述】:

我有一个设置了特定字体和字体大小的 TextView。我想确定多行文本是否适合此 TextView(TextView 的宽度和高度都设置为 match_parent)。 有什么合理的方法吗?

谢谢

【问题讨论】:

  • 是的。您可以使用我发布的解决方案here

标签: android fonts textview


【解决方案1】:

您可以使用这种方法测量文本的边界:

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

然后你就可以得到文本视图的宽度和高度了:

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

【讨论】:

    【解决方案2】:

    如果你把你的 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>
    

    【讨论】:

      猜你喜欢
      • 2013-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多