停止文本在Web地址中的句点上拆分为多行

Wil*_*ill 9 unicode android word-wrap textview

我有一个Android TextView显示一些文本,它是多行的.但是,在文中,我有时会有域名; 如何阻止TextView在其中的句点上拆分行?

例如,是否存在unicode非破坏期?


要查看包装电子邮件地址的操作问题,请运行
android create project --target 16 --path demo --package com.example.demo --activity MainActivity
并将文本更改res/layout/main.xml为" Hello World, MyActivity filler text + email foo@foo.com".这会在Galaxy S3(API级别16)上生成此输出:

邮件包装

(根据需要调整文本以查看具有其他屏幕尺寸的设备上的包装.值得注意的是,在Intellij的布局预览中正确完成包装,它只在设备上发生故障.)

ozb*_*bek 7

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

结果

在Android 4.1.2(Galaxy Nexus)上

使用修复程序在Android 4.1.2上自动换行

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

在此输入图像描述

希望这可以帮助.