检测,其中Android的TextView中会插入一个换行符 [英] Detect where Android's TextView would insert a line break
问题描述
我有一个长方形的瓷砖,我想适应的图片和一些文字到它。图像必须不重叠的文本和图像的尺寸和文本可以变化。
I have a rectangular tile, and I want to fit an image and some text into it. The image must not overlap the text, and the sizes of the image and the text can vary.
进程必须是手工codeD,因为我们有微调是根据我们客户的需求。
The process must be hand-coded, since we have to fine-tune it according to our client's needs.
我试过先使用 getTextBounds()
或 measureText()
,然后测量呈现的文本边界的方法适应的字体大小和图像大小,这样它们不重叠。
I tried the approach of first measuring the rendered text bounds using getTextBounds()
or measureText()
and then adapting the font size and the image size so they don't overlap.
这工作正常,如果文本仅在一行。
This works fine if the text is only on one line.
但是,如果的TextView
环绕文字到多个行,我不能predict文字界限,因为我不知道在哪里的TextView
将插入自动换行。
But if TextView
wraps the text onto multiple line, I cannot predict the text bounds, since I don't know where TextView
would insert the automatic line breaks.
我怎样才能找出其中的TextView
插入自动换行文本的位置?
How can I find out the positions in the text where TextView
inserts an automatic line break?
示例:中的文字
Lorem存有悲坐阿梅德
这将呈现为
| Lorem ipsum |
| dolor sit amet |
我需要一个功能,转换
I need a function that converts
Lorem存有悲坐阿梅德
到
Lorem存有\\ ndolor坐阿梅德
推荐答案
TextView的(布局对象)有你想实现什么一些有用的功能:
TextView (Layout object) has some useful functions for what you are trying to accomplish:
http://developer.android.com/reference/android/text/的layout.html
一起来看看:
getLineCount()
getLineEnd(INT线)
您可以根据所在的字符位于每个lineEnd得到TextView的字符串的子字符串。
You can get the substring for the TextView string based on where the character is located at each lineEnd.
您将需要使用 getViewTreeObserver()
等到TextView的绘制之前,你可以调用这些并从他们那里得到有用的信息。
You will need to use getViewTreeObserver()
to wait until the TextView is drawn before you can call these and get useful information from them.
另外,你可以建立一个自定义的TextView可能通过内置的方法或通过添加一个监听器给它提供数据。该修改文本大小定制的TextView的一个例子是在这里:
Alternatively, you can build a custom TextView that might provide the data through built-in methods or by adding a listener to it. An example of a custom TextView that modifies text size is here:
我已经使用了,做了类似的修改(比如你正在尝试做的)。
I've used that and did similar modifications (like what you are trying to do).
这篇关于检测,其中Android的TextView中会插入一个换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!