android 实际文本宽度

发布于 2024-10-09 12:50:24 字数 713 浏览 0 评论 0原文

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

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

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

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

这就是我正在做的事情。

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

框架中是否有任何库存方法可以精确计算这个?

这是描述我的 TextView 的图像

TextView

I require actual Text width from a TextView.

To expalin this heres my TextView description:

  1. TextView lies in a LinearLayout with some internal padding.
  2. TextView is set to fill the width and wrap up the height.
  3. TextView has a leftCompoundDrawable set and a rightCompoundDrawable set.
  4. Theres also padding given to text of textview from left compound drawable.
  5. Text from text view can be multiline.

What I meant from text from TextView is property of TextView which can be set using android:text property from XML or by calling setText() on TextView.

Heres what I am doing.

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

is there any stock method from framework which can precisely caluclate this ?

Heres an image describing my TextView

TextView

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素衣风尘叹 2024-10-16 12:50:24

试试这个:

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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文