我有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”。产生上的银河S3(API级16)的输出:

“电子邮件包装”

(调整文本适当看到与其它屏幕尺寸的设备包装。值得注意的是,包装是的Intellij的布局预览正确完成,它的唯一的装置,它的故障上。)

有帮助吗?

解决方案

<强> TLDR;

@马特麦克明已经显示出了这个问题解决这里,去抓住它。我只是重新迭代这里的解决方案。


请注意,这个问题已经被固定在安卓4.2.2平台级。见下面的截图为相同的代码库,但是,Galaxy Nexus的不同平台的版本自动换行比较。

“字在Android

因此,如果你的目标不是旧版本的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的)

“上的Android

在的Android 2.3.3(AVD,Nexus之一克隆)

“在这里输入的图像描述”

希望这有助于。

其他提示

要告诉机器人来解析在TextView的域链接在TextView的码块使用此代码:

android:autoLink="web"

这将显示域名作为应用链接并不会分裂线。

使用这样的:

机器人:SINGLELINE = “真”,在XML

对于我来说没有@ozbek的工作液分别@马特麦克明,我不得不改变行

else if(Character.isWhitespace(curChar))

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

但否则很大的溶液,由于

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top