TextView英文自动换行解决方法

TextView组件

遇到一个问题:

想将【TP-LINK_XXXXXXX】已ellipsize="end'的方式展示一部分,设置

android:layout_width="400px"
android:ellipsize="end"
android:maxLines="1"
android:lines="1"

以为显示的结果为:TP-LINK…

结果:TP-…

查了大堆资料,有人说是android源码的缺陷,总结了以下解决方法:

1、将半角转全角(太难看了,难以接受)

2、重写TextView(这个方法确实能解决问题,大神确实多)

3、强大的TextJustify控件(搬运,自测有点卡,小工程不用考虑使用):https://github.com/GitHubSMXY/TextJustify-Android



详情,转载自:

http://www.cnblogs.com/goagent/p/5159125.html




玩法源码(新增了方法):

public class XRTextView extends TextView {
    private boolean mEnabled = true;

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

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

    public XRTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setAutoSplitEnabled(boolean enabled) {
        mEnabled = enabled;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY
                && MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY
                && getWidth() > 0
                && getHeight() > 0
                && mEnabled) {
            String newText = autoSplitText(this);
            if (!TextUtils.isEmpty(newText)) {
                setText(newText);
            }
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private String autoSplitText(final TextView tv) {
        final String rawText = tv.getText().toString(); //原始文本
        final Paint tvPaint = tv.getPaint(); //paint,包含字体等信息
        final float tvWidth = tv.getWidth() - tv.getPaddingLeft() - tv.getPaddingRight(); //控件可用宽度

        //将原始文本按行拆分
        String[] rawTextLines = rawText.replaceAll("\r", "").split("\n");
        StringBuilder sbNewText = new StringBuilder();
        for (String rawTextLine : rawTextLines) {
            if (tvPaint.measureText(rawTextLine) <= tvWidth) {
                //如果整行宽度在控件可用宽度之内,就不处理了
                sbNewText.append(rawTextLine);
            } else {
                //如果整行宽度超过控件可用宽度,则按字符测量,在超过可用宽度的前一个字符处手动换行
                float lineWidth = 0;
                for (int cnt = 0; cnt != rawTextLine.length(); ++cnt) {
                    char ch = rawTextLine.charAt(cnt);
                    lineWidth += tvPaint.measureText(String.valueOf(ch));
                    if (lineWidth <= tvWidth) {
                        sbNewText.append(ch);
                    } else {
                        sbNewText.append("\n");
                        lineWidth = 0;
                        --cnt;
                    }
                }
            }
            sbNewText.append("\n");
        }

        //把结尾多余的\n去掉
        if (!rawText.endsWith("\n")) {
            sbNewText.deleteCharAt(sbNewText.length() - 1);
        }

        return sbNewText.toString();
    }

    public void setFont(String textFont) {
        Typeface lastTypeFace = getTypeface();
        boolean isBold = lastTypeFace != null ? lastTypeFace.isBold() : false;
        Typeface typeface = FontUtil.getTypeface(textFont, getContext());
        //使用字体
        setTypeface(typeface);
        //中文字体设置加粗
        TextPaint paint = getPaint();
        paint.setFakeBoldText(isBold);
    }
}

你可能感兴趣的:(安卓)