8

在我的 Android 布局中,我有一个使用屏幕可用宽度一半的 TextView。在运行时,我将文本设置为(长)电子邮件地址。例如:

googleandroiddeveloper@gmail.com

如果文本不适合一行,Android 会插入一个换行符,这是所需的行为。但是,换行符的位置在第一个不适合该行的字符之前。结果可能是这样的:

googleandroiddeveloper@gmai
l.com

我认为,这有点丑陋,尤其是在电子邮件地址中。我希望换行符出现在@字符之前:

googleandroiddeveloper
@gmail.com

当然,我可以\n在我的strings.xml. 但是,电子邮件地址在每种情况下都会使用两行,即使它适合一行。

我已经认为我找到了一个解决方案,将零宽度空格 ( \u200B) 添加到电子邮件地址。

<string name="email">googleandroiddeveloper\u200B@gmail.com</string>

但除了使用标准空格外,Android 不会将特殊空格字符检测为可破坏空格,因此此时不会添加换行符。

当我在我的应用程序的多个位置处理大量电子邮件地址时,我正在寻找一种解决方案来在@字符之前添加一个易碎且不可见的空间,因此如果电子邮件地址不适合一行,Android 会包装电子邮件地址。

4

2 回答 2

6

@Luksprog 的解决方案非常好,在很多情况下都能解决问题。然而,我在几个方面修改了这个类,让它变得更好。这些是修改:

  • 我使用onSizeChanged而不是onMeasure检查和操作文本,因为使用 with 时会出现onMeasure问题。LinearLayoutlayout_weight
  • getPaddingLeft()我通过使用和考虑了文本的水平填充getPaddingRight()
  • 在测量时afterAt我替换positionposition + 1,否则生成的电子邮件地址包含两个@

代码:

public class EmailTextView extends TextView {

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

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        // the width the text can use, that is the total width of the view minus
        // the padding
        int availableWidth = w - getPaddingLeft() - getPaddingRight();
        String text = getText().toString();
        if (text.contains("\n@")) {
            // the text already contains a line break before @
            return;
        }
        // get the position of @ in the string
        int position = -1;
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == '@') {
                position = i;
                break;
            }
        }
        if (position > 0) {
            final Paint pnt = getPaint();
            // measure width before the @ and after the @
            String beforeAt = text.subSequence(0, position).toString();
            String afterAt = text.subSequence(position + 1, text.length())
                    .toString();
            final float beforeAtSize = pnt.measureText(beforeAt);
            final float afterAtSize = pnt.measureText(afterAt);
            final float atSize = pnt.measureText("@");
            if (beforeAtSize > availableWidth) {
                // the text before the @ is bigger than the width
                // so Android will break it
                return;
            } else {
                if ((beforeAtSize + afterAtSize + atSize) <= availableWidth) {
                // the entire text is smaller than the available width
                    return;
                } else {
                    // insert the line break before the @
                    setText(beforeAt + "\n@" + afterAt);
                }
            }
        }
    }
}

这是EmailTextView与默认值相比的屏幕截图TextView

电子邮件文本视图

对于所有电子邮件地址,它都按我的预期工作。最后一个地址没有改变,因为之前的文本@已经太宽了,所以系统在之前打破了它,因此电子邮件地址无论如何都搞砸了,所以没有必要包含另一个换行符。

于 2012-12-12T14:51:07.027 回答
2

您可以查看TextView下面的自定义类(尽管可能效率不高)在某些情况下应该插入(假设在很少的测试中)所需的换行符:

public static class NewLineText extends TextView {

    private static final String CHALLANGE_TEXT = "\n@";

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        String text = getText().toString();
        if (text.contains(CHALLANGE_TEXT)) {
            return;
        }
        int position = -1;
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == '@') {
                position = i;
                break;
            }
        }
        if (position > 0) {
            final Paint pnt = getPaint();
            String beforeAt = text.subSequence(0, position).toString();
            String afterAt = text.subSequence(position, text.length())
                    .toString();
            final float beforeAtSize = pnt.measureText(beforeAt);
            final float afterAtSize = pnt.measureText(afterAt);
            final float atSize = pnt.measureText("@");
            if (beforeAtSize > getMeasuredWidth()) {
                // return ?! the text before the @ is bigger than the width
                // so Android will break it
                return;
            } else {
                if ((beforeAtSize + afterAtSize + atSize) <= getMeasuredWidth()) {
                    return;
                } else {
                    setText(beforeAt + CHALLANGE_TEXT + afterAt);
                }
            }
        }
    }
}
于 2012-12-05T19:56:35.403 回答