TextView設置文字包含中英文時自動換行問題的終極解決方案

情景,正常TextView中設置文本內容中包含中英文時會形成自動換行的問題,影響界面顯示效果,如圖:java

網上不少解決途徑,甚至有多三方框架處理,可是效果並不能達到,最終是要以下代碼完美解決,效果圖以下:android

具體實現過程 以及代碼

基本思路:先測量TextView的最大可用寬度,而後替換全部的空格符並按行分割,若是小於TextView最大寬度,則不處理;若是大於TextView最大寬度,進行單個字符進行測量,超過最大寬度則加入換行符;git

public class SDAdaptiveTextView extends TextView {
    public SDAdaptiveTextView(Context context) {
        super(context);
    }

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

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

    /**
     * 使用該方法設置TextView的文本內容,改方法不能再主線程中執行
     * @param text
     */
    public void setAdaptiveText(String text) {
        this.setText(text);
        this.setText(adaptiveText(this));
    }
    
    private String adaptiveText(final TextView textView) {
        final String originalText = textView.getText().toString(); //原始文本
        final Paint tvPaint = textView.getPaint();//獲取TextView的Paint
        final float tvWidth = textView.getWidth() - textView.getPaddingLeft() - textView.getPaddingRight(); //TextView的可用寬度
        //將原始文本按行拆分
        String[] originalTextLines = originalText.replaceAll("\r", "").split("\n");
        StringBuilder newTextBuilder = new StringBuilder();
        for (String originalTextLine : originalTextLines) {
            //文本內容小於TextView寬度,即不換行,不做處理
            if (tvPaint.measureText(originalTextLine) <= tvWidth) {
                newTextBuilder.append(originalTextLine);
            } else {
                //若是整行寬度超過控件可用寬度,則按字符測量,在超過可用寬度的前一個字符處手動換行
                float lineWidth = 0;
                for (int i = 0; i != originalTextLine.length(); ++i) {
                    char charAt = originalTextLine.charAt(i);
                    lineWidth += tvPaint.measureText(String.valueOf(charAt));
                    if (lineWidth <= tvWidth) {
                        newTextBuilder.append(charAt);
                    } else {
                        //單行超過TextView可用寬度,換行
                        newTextBuilder.append("\n");
                        lineWidth = 0;
                        --i;//該代碼做用是將本輪循環回滾,在新的一行從新循環判斷該字符
                    }
                }
            }
        }
        return newTextBuilder.toString();
    }
}
  • 使用 setAdaptiveText 方法替代 原生的 setText 方法,注意該方法不能再主線程中執行
  • 若是TextView寬度設置爲WrapContent,爲了測量它的準確寬度,可先使用setText()方法設值,再調用setAdaptiveText()設值
    舉例使用:
tvSdAdaptive.post(new Runnable() {
            @Override
            public void run() {
                tvSdAdaptive.setAdaptiveText("");
            }
        });
tvSdAdaptive.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                tvSdAdaptive.setAdaptiveText("");
                tvSdAdaptive.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });

瞭解更多——GitHub連接github

相關文章
相關標籤/搜索