在EditText中的"word-wrap: break-word"

9

安卓系统是否有类似CSS的属性"word-wrap"?

我只是希望我的文本不会被空格、破折号等所包围,像这样:

  1. hello, w
  2. orld

而不是像这样:

  1. hello,
  2. world
1个回答

8

不幸的是,安卓系统没有这个属性。但你可以用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());

标记为真,因为拐杖总比没有好。 - Raskilas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接