【问题标题】:Avoiding syllabification on URLs避免 URL 上的音节化
【发布时间】:2014-03-12 11:36:57
【问题描述】:

我正在尝试显示这样的文本:

"For more infomration please visit www.my-site.com"

对于某些分辨率/屏幕,文本显示为:

|For more information please visit www.my- |
|site.com                                  |

我可以避免对 URL 部分的这种影响吗?

【问题讨论】:

  • 你能发布你的代码/布局吗?
  • 如果您通常可以将文本放在两行上,请在 URL 前引入换行符 (\n)。
  • 容器的类型是什么?即 TextView、Button 还是其他?
  • 我很确定TextView和Button的效果是一样的。无论如何,我正在使用 TextView。

标签: java android string text word-wrap


【解决方案1】:

使用non-breaking hyphen 代替常规的-

在 XML 中:‑

在 Java 中:"\u2011"

当然,如果你有可点击的链接,不要在 URL 中替换它,而只是链接文本。

例子:

<TextView android:layout_width="60dp" android:layout_height="wrap_content"
    android:text="foo foo-foo foo&#x2011;foo"/>

图形布局:

【讨论】:

    【解决方案2】:

    如果此文本出现在标准 Activity 或 Fragment 中,您可以将其拆分为两个 TextView,第一个包含信息文本,第二个包含网站 URL 并设置属性 android:singleLine="true" 以防止里面的文本被换行.

    接下来,您可以将这两个 TextView 并排放置在自定义的“FlowLayout”中,如果可能,它将显示在一行中:

    |For more information please visit www.my-site.com |  
    |                                                  |
    

    或在 TextView 边界处换行:

    |For more information please visit        |  
    |www.my-site.com                          |
    

    不幸的是,Android 中没有原生的 FlowLayout。您可以编写自己的代码,调整类似 RelativeLayout 的内容,并编写自定义方法来测量屏幕宽度和子视图(有关如何完成此操作的示例,请参见 How to write Android Autowrap Layout using RelativeLayoutLinearLayout Horizontal with wrapping children)。

    或者,您可以使用几种已经可用的此类布局:

    【讨论】:

    • 我不想使用 android:singleLine="true" 或换行符。
    • @Seraphim'shost 你是什么意思没有换行?你想全部在一条线上吗?在这种情况下,您可以将其放置在 ScrollView 中,并允许文本从屏幕上消失,直到用户滚动它。
    • 不想在url前用\n换行
    • 我不建议进行换行,android:singleLine 是用于防止 TextViews 将文本包装在其中的属性,这是默认情况下所做的。另一种解决方案是通过扩展 TextView 创建自定义 View,在其中禁用原始的换行功能。
    • 哦,还有一种可能:您可以根据使用的屏幕大小将 TextView 内的文本大小更改为更大或更小,并确保将大小设置为足够小整条线都适合。
    【解决方案3】:

    如果您的项目要求不允许您使用换行或自定义“FlowLayout”类型的布局,您可以通过使用自定义方法扩展 TextView 来应用自定义换行方案来创建自定义视图。

    // String text must contain a portion between
    //   <unwrappable></unwrappable> tags.
    public void setCustomWrappedText(String text) {
        final String OPEN_TAG = "<unwrappable>";
        final String CLOSE_TAG = "</unwrappable>";
    
        // Unwrappable string not yet set, find it, between <unwrappable></unwrappable>
        // tags, strip out the tags, and set prefix and suffix.
        int index = text.indexOf(OPEN_TAG);
        int index2 = text.indexOf(CLOSE_TAG);
        String prefix = text.substring(0, index);
        String unwrappable = text.substring(index + OPEN_TAG.length(), index2);
        String suffix = text.substring(index2 + CLOSE_TAG.length());
    
        // Contents already fit on one line, do nothing.
        this.setText(prefix + unwrappable + suffix);
        int lines = this.getLineCount();
        if (lines < 2)
            return;
    
        // Set content prefix, ie. text _before_ unwrappable appears, and count lines.
        this.setText(prefix);
        lines = this.getLineCount();
    
        // Set content to prefix _with_ unwrappable (no suffix), and count lines.
        this.setText(prefix + unwrappable);
    
        if (this.getLineCount() > lines) // Text has wrapped inside unwrappable, insert a line break to prevent.
            this.setText(prefix + "\n" + this.unwrappable + suffix);
        else // Text may or may not wrap _after_ unwrappable, we don't care.
            this.setText(prefix + this.unwrappable + suffix);
    }
    

    【讨论】:

    • 使用标记来指定子文本不可包装的解决方案将是完美的。 Somenthign like this: .setText("this is wrappable text and this is not a warappable text")
    • @Seraphim'shost 请看一下,我添加了在&lt;unwrappable&gt;&lt;/unwrappable&gt; 标签之间使用文本的功能。
    • @Seraphim'shost 我明白了。您只想以编程方式设置文本。我为你做了一个自定义方法,它会做 onLayout 以前做的事情,看看。
    • 感谢您的宝贵时间,我的具体问题的解决方案已通过另一种方式解决。我赞成你的回答,因为它很有趣。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-22
    • 2014-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多