问题描述
Android是否具有类似于CSS的属性自动换行"?
Has an android a css-like property "word-wrap"?
我只是想让我的文本不被空格,破折号等包裹,诸如此类:
I just want to my text is not wrapped by spaces, dashes, etc., something like this:
- 你好,w
- 世界
代替
- 你好,
- 世界
推荐答案
不幸的是,android没有此属性.但是您可以使用ReplacementTransformationMethod替换所有破坏字符.
Unfortunately, android hasn't this property. But you can replace all breaking characters with ReplacementTransformationMethod.
class WordBreakTransformationMethod extends ReplacementTransformationMethod
{
private static WordBreakTransformationMethod instance;
private WordBreakTransformationMethod() {}
public static WordBreakTransformationMethod getInstance()
{
if (instance == null)
{
instance = new WordBreakTransformationMethod();
}
return instance;
}
private static char[] dash = new char[] {'-', '\u2011'};
private static char[] space = new char[] {' ', '\u00A0'};
private static char[] original = new char[] {dash[0], space[0]};
private static char[] replacement = new char[] {dash[1], space[1]};
@Override
protected char[] getOriginal()
{
return original;
}
@Override
protected char[] getReplacement()
{
return replacement;
}
}
'\ u2011'是不间断的破折号,'\ u00A0'是不间断的空格.不幸的是,UTF并没有不间断的斜杠('/')模拟,但是您可以使用除斜杠('∕').
'\u2011' is non-breaking dash, '\u00A0' is non-breaking space. Unfortunately, UTF hasn't non-breaking analog for slash ('/'), but you can use division slash (' ∕ ').
要使用此代码,请将WordBreakTransformationMethod的实例设置为您的EditText.
For use this code, set instance of WordBreakTransformationMethod to your EditText.
myEditText.setTransformationMethod(WordBreakTransformationMethod.getInstance());
这篇关于“自动换行:断行"在EditText中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!