【问题标题】:Prevent line-break in TextView防止 TextView 中的换行符
【发布时间】:2020-10-15 12:01:35
【问题描述】:

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

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

aaaaaaa/
bbb-
ccccc/
ddd

但是,除了视图边界处的换行符之外,我想在没有任何换行符的情况下显示它,即像这样:

aaaaaaa/bb
bb-ccccc/d
dd

有什么方法可以禁用自动换行符或转义这些字符?我已经尝试用 \uFEFF 转义但没有成功。

【问题讨论】:

  • 尝试在每个转义字符之前使用 \ 即 bbb\-ccccc
  • 也许这篇文章会对你有所帮助:stackoverflow.com/questions/6134457/…
  • '\' 被称为转义字符..所以在任何你想转义字符的地方使用这个字符,例如 aaaaaaa\/bb。在这种情况下,您通过在前面放置 \ 来转义 /。将相同的规则应用于其他地方
  • 使用 '\\' 或 '\-' 转义不起作用,它会给出“无效的转义序列”错误。

标签: android textview


【解决方案1】:

保留你的 textview 属性

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

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

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

【讨论】:

  • 我认为 OP 正在寻找一个 textView 宽度恒定的解决方案。您的回答没有说​​明这一点
  • 这解决了我的问题!
【解决方案2】:

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

    这是 Android 6 中的新事物。

    尝试将此添加到您的 TextView xml 布局中

    android:hyphenationFrequency="none"
    

    【讨论】:

      【解决方案4】:

      这是我在 kotlin 中的工作

      object WordBreakTransformationMethod : ReplacementTransformationMethod() {
          private val dash = charArrayOf('-', '\u2011')
          private val space = charArrayOf(' ', '\u00A0')
          private val slash = charArrayOf('/', '\u2215')
      
          private val original = charArrayOf(dash[0], space[0], slash[0])
          private val replacement = charArrayOf(dash[1], space[1], slash[1])
      
          override fun getOriginal() = original
          override fun getReplacement() = replacement
      }
      
      //tv_text is TextView
      tv_text.apply {
          transformationMethod = WordBreakTransformationMethod
          text = item.text
      }
      

      【讨论】:

      • 解决了我在 textview 上的问题,其宽度为:wrap_contents,max_width:200dp,gravity_center,位于相对布局内。谢谢。
      【解决方案5】:

      Android TextView 遵循标准的 Unicode 换行算法:http://www.unicode.org/reports/tr14/tr14-45.html 摘录:/之前防止休息,之后允许休息

      您可以通过在斜杠后放置“单词连接符”字符 (U+2060) 来解决此问题。

      来自 strings.xml 的示例: aaaaaaa/\u2060bbb-ccccc/\u2060ddd

      您也可以尝试使用 android:breakStrategy="balanced" 来保持线条大致相同的长度。

      【讨论】:

        【解决方案6】:

        没有现成的解决方案,也没有“在 TextView 中按字母换行”之类的东西,唯一的好方法是扩展 TextView 并修改 Paint 的 breakText(String text, boolean measureForwards, float maxWidth, float[ ] 测量宽度) 函数。

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

        【讨论】:

          【解决方案7】:

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

          【讨论】:

          • 设置行似乎可以工作,但我如何计算动态文本所需的行数?我试过 getLineCount() 但这会返回 0 直到视图被渲染。我试图用持有的 textSize 来计算所需行的数量,但在我知道 TextView 的宽度并且在渲染之前宽度为 0 之前,我无法完全计算它......似乎我在绕圈子。
          • 在创建之前,您无法获得任何视图的宽度。如果您在活动中,请在 OnPostCreate 中进行宽度计算。你可以在那里得到它的宽度。 @user3190568
          【解决方案8】:

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

              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);
          

          现在文本不会转义

          【讨论】:

            【解决方案9】:

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

            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
            

            根据这些值,您可以将文本分成几部分并插入换行符。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-10-23
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-01-29
              相关资源
              最近更新 更多