我如何确定有多少文字将适合在Android的一个TextView?有多少、适合、文字、TextView

2023-09-06 00:29:01 作者:爱情废柴

我有一个布局,看起来是这样的:

I have a layout that looks something like this:

[TextView 1] [TextView 2]
[ TextView 2 spill-over ]

从本质上讲,我需要的TextView 2的内容换到下一行,但地方开始的TextView 1开始。我在想,如果我知道有多少文本将融入的TextView 2它运行的空间上线的人之前,我可以采取文字的其余部分,把它放在另一个TextView中的前两个下方。所以,我需要多少来衡量的文字将适用于一个TextView(这可能会非常棘手,因为据我所知,Android将尝试在一个良好的位置,打破一个TextView的文本,以便它不会破字如果能够避免或中间),我需要就如何打好这一点新的想法。

Essentially, I need the contents of TextView 2 to wrap to the next line, but start where TextView 1 starts. I was thinking that if I knew how much text would fit into TextView 2 before it runs out of space on line one, I could take the rest of the text and put it in another TextView below the first two. So I need to measure how much text will fit into a TextView (which can be tricky because as far as I can tell, Android will try to break the text in a TextView at a good location so that it won't break a word in the middle if it can be avoided) or I need a new idea on how to lay this out.

任何帮助将是很大的AP preciated。

Any help would be greatly appreciated.

在此先感谢,

groomsy

推荐答案

不幸的是,Paint.breakText并在被认为在我的两行的TextView不返回完全相同的结果。

Unfortunately, Paint.breakText didn't return the exact same result as in was seen in my two-line TextView.

不过,这个工作

int numChars = textView.getLayout().getLineEnd(1);

(使用numberOfLines - 1作为参数传递给它)

(use numberOfLines - 1 as the parameter to it)

参考 http://developer.android.com/reference/android/text/Layout.html#getLineEnd(int) Set TextView中的各行不同的宽度