安卓系统是否有类似CSS的属性"word-wrap"?
我只是希望我的文本不会被空格、破折号等所包围,像这样:
- hello, w
- orld
而不是像这样:
- hello,
- world
安卓系统是否有类似CSS的属性"word-wrap"?
我只是希望我的文本不会被空格、破折号等所包围,像这样:
而不是像这样:
不幸的是,安卓系统没有这个属性。但你可以用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没有针对斜杠('/')的不换行模拟符,但可以使用除法斜杠('∕')。
要使用此代码,请将WordBreakTransformationMethod的实例设置为您的EditText。
myEditText.setTransformationMethod(WordBreakTransformationMethod.getInstance());