防止TextView中的换行符

use*_*568 11 android textview

在我的Android应用程序中,我有一个文本视图,显示包含特殊字符的文本.TextView以某种方式自动打破字符'/'和' - '的字符串.

例如,字符串"aaaaaaa/bbb-ccccc/ddd"显示为

aaaaaaa/
bbb-
ccccc/
ddd
Run Code Online (Sandbox Code Playgroud)

但是,我想显示它没有任何换行符,除了在视图边界的那个,即,像这样:

aaaaaaa/bb
bb-ccccc/d
dd
Run Code Online (Sandbox Code Playgroud)

有没有办法停用自动换行符或转义这些字符?我已经尝试用\ uFEFF转义而没有成功.

小智 5

保留textview属性

android:layout_width="wrap_content"
android:layout_height="wrap_content"
Run Code Online (Sandbox Code Playgroud)

在string.xml中定义您的字符串

<string name="Username"> aaaaaaa\/bb\nbb\-ccccc\/d\ndd</string>
Run Code Online (Sandbox Code Playgroud)

  • 我认为 OP 正在寻找 textView 宽度恒定的解决方案。你的回答没有考虑到这一点 (3认同)

Ser*_*us7 5

也许这是一个解决方案:https : //stackoverflow.com/a/22337074/3472905

我已经添加了前面提到的斜线:

public 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[] slash = new char[]{'/', '\u2215'};

    private static char[] original = new char[]{dash[0], space[0], slash[0]};
    private static char[] replacement = new char[]{dash[1], space[1], slash[1]};

    @Override
    protected char[] getOriginal() {
        return original;
    }

    @Override
    protected char[] getReplacement() {
        return replacement;
    }
}
Run Code Online (Sandbox Code Playgroud)


Bud*_*ius 0

您可能可以使用Lines属性或其对应方法setLines(int)