问题 防止TextView中的换行符


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

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

aaaaaaa/
bbb-
ccccc/
ddd

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

aaaaaaa/bb
bb-ccccc/d
dd

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


7937
2018-01-13 14:58


起源

尝试在每个转义字符之前使用\,即bbb \ -ccccc - Saqib
也许这篇文章会帮助你: stackoverflow.com/questions/6134457/... - George Theodorakis
'\'被称为转义字符。因此,无论您想要转义哪个字符,例如aaaaaaa \ / bb,都要使用此字符。在这种情况下,你正在逃避/放置\之前。将相同的规则应用于其他地方 - Ramakishna Balla
使用'\\'或'\ - '进行转义不起作用,它会产生“无效的转义序列”错误。 - user3190568


答案:


保留textview属性

android:layout_width="wrap_content"
android:layout_height="wrap_content"

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

<string name="Username"> aaaaaaa\/bb\nbb\-ccccc\/d\ndd</string>

5
2018-04-12 07:20



我认为OP正在寻找textView宽度不变的解决方案。你的答案没有考虑到这一点 - suku


也许这是一个解决方案: 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;
    }
}

3
2018-04-12 09:01





你可能可以使用  属性或其对应部分方法 setLines(INT)


1
2018-01-13 15:07



设置线条似乎有效,但我如何计算动态文本所需的线条数量?我尝试了getLineCount()但是返回0直到呈现视图。我试着用textSize的持有来计算所需行的数量但我无法完全计算它,直到我知道TextView的宽度并且宽度为0直到渲染...似乎我要进入圆圈。 - user3190568
你不能得到任何视图的宽度,直到它被创建。如果您在活动中,请在OnPostCreate中进行宽度计算。你可以在那里得到它的宽度。 @ user3190568 - Omid Heshmatinia


没有现成的解决方案,也没有“在TextView中用文字换文本”这样的事情,以一种好的方式做到这一点的唯一方法是扩展TextView并修改Paint的breakText(String text,boolean measureForwards,float maxWidth,float [] measuredWidth)功能。

此外,您可以计算TextView大小(以像素为单位),计算一个字母的宽度(以像素为单位),然后找到适合一行的字母数(X),然后在每个X字母后插入换行符


1
2018-04-12 14:02





我测试了以下代码。您甚至可以将其转换为函数:

    String specialString = "a/b/-c/d-d";
    String[] specialArray = specialString.split("/");
    String str = "";
    for(int i = 0; i < specialArray.length - 1; i++){
        str = str + specialArray[i] + Character.toString((char) 47);
    }
    str = str + specialArray[specialArray.length - 1];
    specialArray = str.split("-");
    str = "";
    for(int i = 0; i < specialArray.length - 1; i++){
        str = str + specialArray[i] + Character.toString((char) 45);
    }
    str = str + specialArray[specialArray.length - 1];
    textView.setText(str);

现在文本没有逃脱


0
2018-04-06 05:41





您可以这样计算文本的大小:

String text = "This is my text";
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setTextSize(14.0f);
Rect bounds = new Rect();
textPaint.getTextBounds(text, 0, text.length(), bounds);

bounds.width() // width in pixels
bounds.height() // height in pixels

根据这些值,您可以分解文本并插入换行符。


0
2017-07-02 10:14





它是Android 6中的新功能。

尝试将其添加到TextView xml布局中

android:hyphenationFrequency="none"

0
2017-09-21 07:50





你可以试试这个:

"aaaaaaa"+"/"+"bbb-ccccc"+"/"+"ddd"

-1
2018-04-11 20:48



绝对不行。编译器连接字符串。 - hgoebl