3

我需要来自 TextView 的实际文本宽度。

为了解释这一点,这是我的 TextView 描述:

  1. TextView 位于带有一些内部填充的 LinearLayout 中。
  2. TextView 设置为填充宽度并包裹高度。
  3. TextView 有一个 leftCompoundDrawable 集和一个 rightCompoundDrawable 集。
  4. 从左侧复合可绘制对象中还为 textview 的文本提供了填充。
  5. 文本视图中的文本可以是多行的。

我从TextView中的文本的意思是 TextView的属性,可以使用XML 中的属性或通过在 TextView 上调用 setText() 来设置。android:text

这是我在做什么。

[ScreenWidth]-[LeftCompundDrawableIntrinsic Width]-[RightDrawabaleIntrinsicWidth]-[LeftCompoundDrawablePadding]

是否有任何来自框架的库存方法可以精确计算?

这是描述我的 TextView 的图像

文本视图

4

1 回答 1

0

Try this:

TextView tv;
Rect rc=new Rect();
//...
for(int i=0; i < tv.getLineCount(); i++)
{
   tv.getLineBounds(line, rc);
   Log.v(TAG, "Line #"+i+", left="+rc.left+", right="+rc.right);
}
于 2010-12-29T13:16:01.557 回答