阻止文本在网址中的句点上拆分为多行

发布于 2024-10-03 02:59:43 字数 639 浏览 4 评论 0原文

我有一个显示一些文本的 Android TextView,它是多行的。不过,在文中,我有时也会有域名;如何阻止 TextView 将行按句点分割?

例如,是否有 unicode 不间断句点?


要查看包装电子邮件地址时出现的问题,请运行
android 创建项目 --target 16 --path demo --package com.example.demo --activity MainActivity
并将 res/layout/main.xml 中的文本更改为“Hello World, MyActivity 填充文本 + 电子邮件 [电子邮件受保护]”。这会在 Galaxy S3(API 级别 16)上产生以下输出:

Emailwrapping

(根据需要调整文本以查看设备上的换行值得注意的是,在 Intellij 的布局预览中,换行是正确的,仅在设备上出现错误。)

I have an Android TextView displaying some text, and it's multi-line. However, in the text, I sometimes have domain names; how can I stop the TextView from splitting the lines up on the periods in them?

Is there a unicode non-breaking-period, for example?


To see the issue in action in wrapping an email address, run
android create project --target 16 --path demo --package com.example.demo --activity MainActivity
and change the text in res/layout/main.xml to "Hello World, MyActivity filler text + email [email protected]". That produces this output on a Galaxy S3 (API level 16):

Email wrapping

(Adjust text as appropriate to see wrapping on devices with other screen sizes. Notably, the wrapping is done correctly in Intellij's layout preview, it's only on the device that it's faulty.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

零崎曲识 2024-10-10 02:59:43

TLDR;

@Matt McMinn 已经在此处展示了此问题的解决方案,快去获取吧。我只是在这里重申该解决方案。


请注意,此问题已在 Android 4.2.2 中的平台级别得到修复。请参阅下面的屏幕截图,了解 Galaxy Nexus 上相同代码库但不同平台版本的自动换行比较。

Android 4.1.2 与 Android 4.2.2 上的自动换行比较

因此,如果您不针对旧版本的 Android,则可能根本不想使用此修复程序。

代码

MainActivity.java

package com.example.nobr;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.TextView.BufferType;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView helloWorld = (TextView) findViewById(R.id.hello_world);
        helloWorld.setText(R.string.hello_world, BufferType.EDITABLE);

        TextView longText = (TextView) findViewById(R.id.long_text);
        longText.setText(R.string.long_text_with_url, BufferType.EDITABLE);
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp" >

    <com.example.nobr.NonBreakingPeriodTextView
        android:id="@+id/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.example.nobr.NonBreakingPeriodTextView
        android:id="@+id/long_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/hello_world"
        android:layout_below="@+id/hello_world"
        android:layout_marginTop="20dp" />

</RelativeLayout>

NonBreakingPeriodTextView.java

package com.example.nobr;

import android.content.Context;
import android.graphics.Paint;
import android.text.Editable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

public class NonBreakingPeriodTextView extends TextView {
    private static final String TAG = "NonBreakingPeriodTextView";

    public NonBreakingPeriodTextView(Context context) {
        super(context);
    }

    public NonBreakingPeriodTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        Editable editable = getEditableText();
        if (editable == null) {
            Log.d(TAG, "non-editable text");
            return;
        }
        int width = getWidth() - getPaddingLeft() - getPaddingRight();
        if (width == 0) {
            Log.d(TAG, "zero-length text");
            return;
        }

        Paint p = getPaint();
        float[] widths = new float[editable.length()];
        p.getTextWidths(editable.toString(), widths);
        float curWidth = 0.0f;
        int lastWSPos = -1;
        int strPos = 0;
        final char newLine = '\n';
        final String newLineStr = "\n";
        boolean reset = false;
        int insertCount = 0;

        /*
         * Traverse the string from the start position, adding each character's width to the total
         * until: 1) A whitespace character is found. In this case, mark the whitespace position. If
         * the width goes over the max, this is where the newline will be inserted. 2) A newline
         * character is found. This resets the curWidth counter. curWidth > width. Replace the
         * whitespace with a newline and reset the counter.
         */

        while (strPos < editable.length()) {
            curWidth += widths[strPos];

            char curChar = editable.charAt(strPos);

            if (curChar == newLine) {
                reset = true;
            } else if (Character.isWhitespace(curChar)) {
                lastWSPos = strPos;
            } else if (curWidth > width && lastWSPos >= 0) {
                editable.replace(lastWSPos, lastWSPos + 1, newLineStr);
                insertCount++;
                strPos = lastWSPos;
                lastWSPos = -1;
                reset = true;
            }

            if (reset) {
                curWidth = 0.0f;
                reset = false;
            }

            strPos++;
        }

        if (insertCount != 0) {
            setText(editable);
        }
    }
}

结果

在Android 4.1.2 (Galaxy Nexus)上< /strong>

修复后的 Android 4.1.2 上的自动换行

在 Android 2.3.3 上(AVD、Nexus One克隆)

在此处输入图像描述

希望这会有所帮助。

TLDR;

@Matt McMinn has already shown a solution for this problem here, go grab it. I am only re-iterating that solution here.


Note that, this issue has already been fixed at platform level in Android 4.2.2. See the below screenshots for word wrap comparison for the same code base but different platform versions on Galaxy Nexus.

Word wrap comparison on Android 4.1.2 vs Android 4.2.2

Hence, if you are not targeting older versions of Android, you may not wish to use this fix at all.

The code

MainActivity.java:

package com.example.nobr;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.TextView.BufferType;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView helloWorld = (TextView) findViewById(R.id.hello_world);
        helloWorld.setText(R.string.hello_world, BufferType.EDITABLE);

        TextView longText = (TextView) findViewById(R.id.long_text);
        longText.setText(R.string.long_text_with_url, BufferType.EDITABLE);
    }
}

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp" >

    <com.example.nobr.NonBreakingPeriodTextView
        android:id="@+id/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.example.nobr.NonBreakingPeriodTextView
        android:id="@+id/long_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/hello_world"
        android:layout_below="@+id/hello_world"
        android:layout_marginTop="20dp" />

</RelativeLayout>

NonBreakingPeriodTextView.java:

package com.example.nobr;

import android.content.Context;
import android.graphics.Paint;
import android.text.Editable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

public class NonBreakingPeriodTextView extends TextView {
    private static final String TAG = "NonBreakingPeriodTextView";

    public NonBreakingPeriodTextView(Context context) {
        super(context);
    }

    public NonBreakingPeriodTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        Editable editable = getEditableText();
        if (editable == null) {
            Log.d(TAG, "non-editable text");
            return;
        }
        int width = getWidth() - getPaddingLeft() - getPaddingRight();
        if (width == 0) {
            Log.d(TAG, "zero-length text");
            return;
        }

        Paint p = getPaint();
        float[] widths = new float[editable.length()];
        p.getTextWidths(editable.toString(), widths);
        float curWidth = 0.0f;
        int lastWSPos = -1;
        int strPos = 0;
        final char newLine = '\n';
        final String newLineStr = "\n";
        boolean reset = false;
        int insertCount = 0;

        /*
         * Traverse the string from the start position, adding each character's width to the total
         * until: 1) A whitespace character is found. In this case, mark the whitespace position. If
         * the width goes over the max, this is where the newline will be inserted. 2) A newline
         * character is found. This resets the curWidth counter. curWidth > width. Replace the
         * whitespace with a newline and reset the counter.
         */

        while (strPos < editable.length()) {
            curWidth += widths[strPos];

            char curChar = editable.charAt(strPos);

            if (curChar == newLine) {
                reset = true;
            } else if (Character.isWhitespace(curChar)) {
                lastWSPos = strPos;
            } else if (curWidth > width && lastWSPos >= 0) {
                editable.replace(lastWSPos, lastWSPos + 1, newLineStr);
                insertCount++;
                strPos = lastWSPos;
                lastWSPos = -1;
                reset = true;
            }

            if (reset) {
                curWidth = 0.0f;
                reset = false;
            }

            strPos++;
        }

        if (insertCount != 0) {
            setText(editable);
        }
    }
}

The result

On Android 4.1.2 (Galaxy Nexus)

Word wrap on Android 4.1.2 with the fix

On Android 2.3.3 (AVD, Nexus One clone)

enter image description here

Hope this helps.

神经大条 2024-10-10 02:59:43

要告诉 android 解析 TextView 中的域链接,请在 TextView 代码块中使用以下代码:

android:autoLink="web"

这会将域名显示为应用程序中的链接,并且不会拆分行。

To tell android to parse the domain links in the TextView use this code in the TextView code block:

android:autoLink="web"

This will show the domain names as links in the app and wont split the lines.

廻憶裏菂餘溫 2024-10-10 02:59:43

在 xml 中使用:

android:singleLine="true"

Use this :

android:singleLine="true" in xml

一抹微笑 2024-10-10 02:59:43

对我来说,@ozbek 的解决方案分别是@Matt McMinn,我不得不换行,

else if(Character.isWhitespace(curChar))

} else if (curChar == '\u00A0') {

除此之外还有很好的解决方案,谢谢

for me didn't work solution of @ozbek respectively @Matt McMinn, I had to change line

else if(Character.isWhitespace(curChar))

for

} else if (curChar == '\u00A0') {

but otherwise great solution, thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文