Textview显示中英数字混合的文字时换行不美观

解决方法如下:

import android.content.Context;
import android.graphics.Paint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.ViewTreeObserver;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

/**
 * 解决TextView设置中英文自动换行留空白问题
 * https://blog.csdn.net/SilenceOO/article/details/112760170
 */
public class NormalTextView extends AppCompatTextView {

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

    public NormalTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public NormalTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 设置文本,解决中英文自动换行留空白问题
     *
     * @param text 要显示的文本
     */
    public void setAdaptiveText(String text) {
        this.setText(text);
        this.initAutoSplitTextView();
    }

    private void initAutoSplitTextView() {
        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                getViewTreeObserver().removeOnGlobalLayoutListener(this);
                final CharSequence newText = autoSplitText(NormalTextView.this);
                if (!TextUtils.isEmpty(newText)) {
                    setText(newText);
                }
            }
        });
    }

    //返回CharSequence对象
    private CharSequence 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 newStringBuilder = new StringBuilder();
        for (String rawTextLine : rawTextLines) {
            if (tvPaint.measureText(rawTextLine) <= tvWidth) {
                //如果整行宽度在控件可用宽度之内,就不处理了
                newStringBuilder.append(rawTextLine);
            } else {
                //如果整行宽度超过控件可用宽度,则按字符测量,在超过可用宽度的前一个字符处手动换行
                float lineWidth = 0;
                int startIndex = 0;
                int endIndex = 0;
                for (int cnt = 0; cnt != rawTextLine.length(); ++cnt) {
                    char ch = rawTextLine.charAt(cnt);
                    //lineWidth += tvPaint.measureText(String.valueOf(ch));
                    endIndex = cnt + 1;
                    lineWidth = tvPaint.measureText(rawTextLine.substring(startIndex, endIndex));
                    if (lineWidth <= tvWidth) {
                        newStringBuilder.append(ch);
                    } else {
                        newStringBuilder.append("\n");
                        startIndex = cnt;
                        lineWidth = 0;
                        --cnt;
                    }
                }
            }
            newStringBuilder.append("\n");
        }

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

        return newStringBuilder;
    }
}

优化前:

优化后:

你可能感兴趣的:(android)