如何控制时的TextView字符串发送到新的生产线? [英] How to control when TextView sends String to new line?

查看:109
本文介绍了如何控制时的TextView字符串发送到新的生产线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一个字一个字符串的TextView 是太大,无法在同一行前也就是说它跳到下一行。这被证明是非常有用的。然而,这使我处于一个两难境地。我的字符串,对我的特殊原因,需要有一个空间,一个单词的每个字母和单词之间的两个空间之间。正因为这样的的TextView 将其拆分在一行一块,并在下次另一片字。所以,我想我可能要创建一个自定义查看扩展的TextView 和覆盖它跳到方式下一行。但是,如果现在看为TextView的class文件我找不到一种方法,我会做到这一点。因此,任何帮助,意见或建议,将大大AP preciated!谢谢!

When a word in a String from a TextView is too large to fit on the same line as the prior words it skips to the next line. This proves to be very useful. However, this puts me in a dilemma. My String, for my particular reasons, needs to have a space in between each letter of a word and two spaces in between words. Because of this the TextView will split words having one piece on one line and the other piece on the next. So, I'm thinking I may have to create a custom View that extends TextView and override the way it skips to the next line. But taking a look at the class documents for textview I can't find a way I would do this. So, any help, advice or suggestions will be greatly appreciated! Thanks!

推荐答案

您可以覆盖的TextView 有一些修改。

You can override TextView with a few modifications.

其基本战略是覆盖的onDraw()方法和定制油漆文本。您还可以重写 onMeasure()

The basic strategy will be to override the onDraw() method and custom paint your text. You will also have to override onMeasure().

的onDraw()你将pre-过程中您的字符串自定义的的并插入换行符( \\ n )是必要的。

Within onDraw() you will pre-process the custom words in your string and insert newline characters (\n) as necessary.

循环通过的的在你的文字,比较<一个href=\"http://developer.android.com/reference/android/graphics/Paint.html#measureText%28java.lang.String%29\"相对=nofollow> measureText(字符串)与当前宽度与的TextView 来确定要插入新行。不要忘了考虑填充。

Loop through the words in your text and compare measureText(String) with the current width of your TextView to determine where to insert the newlines. Don't forget to take into account "padding".

示例

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // TODO Auto-generated method stub
    super.onMeasure(getSuggestedMinimumWidth(), getSuggestedMinimumHeight());
}

@Override
protected void onDraw(Canvas canvas) {
    // preprocess your text
    canvas.drawText(...);
}

这篇关于如何控制时的TextView字符串发送到新的生产线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆