仅在必要时在特定位置添加换行符

Mat*_*ers 8 java android android-layout android-xml

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

googleandroiddeveloper@gmail.com
Run Code Online (Sandbox Code Playgroud)

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

googleandroiddeveloper@gmai
l.com
Run Code Online (Sandbox Code Playgroud)

我认为,这有点难看,特别是在电子邮件地址中.我希望换行符出现在@角色前面:

googleandroiddeveloper
@gmail.com
Run Code Online (Sandbox Code Playgroud)

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

我已经认为我已经找到了在\u200B电子邮件地址中添加ZERO WIDTH SPACE()的解决方案.

<string name="email">googleandroiddeveloper\u200B@gmail.com</string>
Run Code Online (Sandbox Code Playgroud)

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

由于我在应用程序的多个位置处理大量电子邮件地址,我正在寻找在@角色之前添加易碎和不可见空间的解决方案,因此如果不适合一行,Android将包装电子邮件地址.

Mat*_*ers 6

@ Luksprog的解决方案非常好,在许多情况下解决了这个问题.但是,我在几个点修改了类,使其更好.这些是修改:

  • 我用onSizeChanged的,而不是onMeasure用于检查和操作文本,因为有与问题,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);
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是EmailTextView与默认值相比的屏幕截图TextView:

EmailTextView

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