首页 > 解决方案 > 如何在 TextView 中禁用自动换行?

问题描述

我有一个TextView包含 60 个字符的随机字符串。

该字符串是一个单词,不包含任何空格。

问题是在某些字符(例如@, &, %)之后,我得到了一个我不想要的自动换行符。

我想要的结果是每一行都填满到最后,并且不存在随机换行符。

我试过设置breakStrategy和更新hyphenationFrequency,但这没有帮助。

我怎样才能防止这种情况发生?

在此处输入图像描述

更新:感谢@Darkman,这是我写的解决方案。它检查在没有换行符的情况下一行可以容纳多少个字符并\n在末尾追加。

请注意,对于我的用例字符串没有空格,并且我使用的是等宽字体。

fun TextView.toNonBreakingString(text: String?): String {
    if (text == null) return ""
    val container = parent as? ViewGroup ?: return text

    val lineWidth = (container.width - container.paddingStart - container.paddingEnd).toFloat()
    val maxCharsInOneLine = paint.breakText(text, 0, text.length, true, lineWidth, null)
    if (maxCharsInOneLine == 0) return text

    val sb = StringBuilder()
    var currentLine = 1
    var end = 0
    for (i in 0..text.count() step maxCharsInOneLine) {
        end = currentLine * maxCharsInOneLine
        if (end > text.length) end = text.length
        sb.append(text.subSequence(i, end))
        sb.append("\n")
        currentLine = currentLine.inc()
    }

    if (end < text.length) {
        val remainingChars = text.length - end
        sb.append(text.takeLast(remainingChars))
    }

    return sb.toString()
}

标签: androidandroid-layouttextviewline-breaks

解决方案


实现所需结果的一种方法是使用monospace字体、左重力和一点点编程。

<TextView
    android:layout_height="wrap_content"
    android:layout_width="200dp"
 
   android:text="akAOCYMKIpVmSwhHtWS%fBFK7eWi%19a590344gZqGQtkTcRv^1lFqH#F@Lhr"
    android:gravity="left"
    android:background="#708BCD"
    android:id="@+id/tv"
    android:textSize="20sp"
    android:typeface="monospace"/>
//Note :: Auto-updated
public final void wrapText(final TextView textView, final String text)
{
    final float textSize = (textView.getTextSize() / 250F) * 150F;
    final int textLen = text.length();
    final int columns = (int)(textView.getWidth() / textSize);
    final int lines = (int)((float) textLen / columns);
    final StringBuilder sb = new StringBuilder(textLen + lines);
    for(int i=0, n=0; i < textLen; i+=columns, ++n) {
        sb.append(text.subSequence(i, Math.min(textLen, i + columns)));
        if(n<lines) sb.append("\n");
    }
    textView.setText(sb.toString());
}

或者

//Note :: Auto-updated
public final void wrapText(final TextView textView)
{
    final float textSize = (textView.getTextSize() / 250F) * 150F;
    final CharSequence text = textView.getText();
    final int textLen = text.length();
    final int columns = (int)(textView.getWidth() / textSize);
    final int lines = (int)((float) textLen / columns);
    final StringBuilder sb = new StringBuilder(textLen + lines);
    for(int i=0, n=0; i < textLen; i+=columns, ++n) {
        sb.append(text.subSequence(i, Math.min(textLen, i + columns)));
        if(n<lines) sb.append("\n");
    }
    textView.setText(sb.toString());
}

或者

// Note :: Self-update
public final String wrapText(final TextView textView, final String text)
{
    final float textSize = (textView.getTextSize() / 250F) * 150F;
    final int textLen = text.length();
    final int columns = (int)(textView.getWidth() / textSize);
    final int lines = (int)((float) textLen / columns);
    final StringBuilder sb = new StringBuilder(textLen + lines);
    for(int i=0, n=0; i < textLen; i+=columns, ++n) {
        sb.append(text.subSequence(i, Math.min(textLen, i + columns)));
        if(n<lines) sb.append("\n");
    }
    return sb.toString();
}

// Implementation:
// textView.setText(wrapText(textView, "akAOCYMKIpVmSwhHtWS%fBFK7eWi%19a590344gZqGQtkTcRv^1lFqH#F@Lhr"));

并且您需要在测量布局后调用其中一种方法。这意味着在 之外onCreate()onCreateView()或类似的东西。

在此处输入图像描述


推荐阅读