android - 如何禁用 TextView 中的自动换行?

标签 android android-layout textview line-breaks

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

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

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

我想要的结果是每一行都被填满,并且没有随机的换行符。

我已经尝试设置 breakStrategy 并更新 hyphenationFrequency,但这没有帮助。

如何防止这种情况发生?

enter image description here

更新:感谢@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()
}

最佳答案

实现所需结果的一种方法是使用 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() 或类似的东西之外。

enter image description here

关于android - 如何禁用 TextView 中的自动换行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67919361/

相关文章:

java - 我正在尝试创建一个 Android 应用程序,它使用符号链接(symbolic link)在另一个应用程序中复制文件并在中央 Android 日志中打印

android - ORMLite 多对多关系

java - 二维数组复制列和行,并证明包含是否等于

android - 键盘重叠自定义编辑文本

java - 在 onclick 方法中实现进度条时出现问题

android - 如何使textview可点击

java - 作为计数器,以定时间隔将值从 1 美元增加到 125 万美元

android - 无法使用 Android Studio 构建的 apk 通过 Android Studio 或 adb 安装 apk

java - 我正在尝试启动应用程序,但它崩溃了

android - 如何在段落的单词之间添加空格以在 Android 中的 Textview(即对齐文本)中删除结尾的空格?