【问题标题】:check if String will fit inside textview检查字符串是否适合文本视图
【发布时间】:2014-08-03 12:17:31
【问题描述】:

我在代码中使用以下 sn-p 来检查字符串是否足够小以适合文本视图。

private int findLengthThatFits (TextView view, String text) {
    float textWidth = view.getPaint().measureText(text);
    if (textWidth >= view.getMeasuredWidth ()){
      //do somethings
    }
    //other stuff
}

但是view.getMeasuredWidth () 不断返回0.0。有谁知道我可以如何解决这个问题?我使用How to find android TextView number of characters per line? 作为参考。

【问题讨论】:

    标签: android text textview size width


    【解决方案1】:

    不久前我也遇到过同样的问题。就我而言,我试图计算 textview 占用的行数。问题是 view.getMeasuredWidth () 之类的方法只能在 textview 上调用 requestLayout() 之后使用。为了解决这个问题,viewtreeobserver 用于确保仅在适当的时间调用方法。我不想使用它,所以我使用了这篇文章中的Ellipsizing Textview。它就像一个魅力,还有一个界面可以知道提供的文本是否被省略了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-30
      • 2020-04-15
      • 1970-01-01
      • 1970-01-01
      • 2014-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多