解決部分場景TextView換行對齊

背景

原生textview換行會出現(xiàn)換行踢俄,右側(cè)文字參差不齊缩功,最近公司產(chǎn)品經(jīng)理看不下去了,要求盡可能要對齊都办,保持美觀嫡锌。通過我花了一些時間來研究, 發(fā)現(xiàn)大部分原因是因為句子中含有中英文標(biāo)點符號脆丁,可以通過替換中文標(biāo)點符號解決世舰,文字半角轉(zhuǎn)換為全角,對于一些頑固的情況槽卫, 通過繪制寬度來解決跟压,最終達(dá)成自己要想的效果。

效果

原生換行:

yuansheng.png

自定義textview換行:

zidingyi.png
替換中文符號:
public String stringFilter(String str) {
        str = str.replaceAll("【", "[").replaceAll("】", "]").replaceAll("歼培!", "!").replaceAll(":", ":");// 替換中文標(biāo)號
        String regEx = "[『』]"; // 清除掉特殊字符
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }
半角轉(zhuǎn)換為全角:
public String ToDBC(String input) {
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 12288) {// 全角空格為12288震蒋,半角空格為32
                c[i] = (char) 32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375)// 其他字符半角(33-126)與全角(65281-65374)的對應(yīng)關(guān)系是:均相差65248
                c[i] = (char) (c[i] - 65248);
        }
        return new String(c);
    }

測試代碼

1.布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.lenovo.mytest1708.SecondActivity">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:maxWidth="200dp"
        android:text="原生textview"
        android:visibility="gone"
        android:layout_centerInParent="true"
        android:layout_height="wrap_content"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:orientation="vertical"
        android:visibility="visible"
        android:layout_centerInParent="true"
        android:layout_height="wrap_content">

        <View
            android:layout_width="match_parent"
            android:background="#beecd3"
            android:layout_height="20dp"/>

        <com.example.lenovo.mytest1708.view.CustomAlignTextView
            android:id="@+id/text001"
            android:maxWidth="200dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="自定義textview"
            android:background="#beecd3"
            android:visibility="visible"/>

        <View
            android:layout_width="match_parent"
            android:background="#beecd3"
            android:layout_height="20dp"/>

    </LinearLayout>

</RelativeLayout>

2.自定義textview:

package com.example.lenovo.mytest1708.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.view.View;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by blueocean on 2017/8/23.
 * desc: 自定義換行對齊textview
 */

public class CustomAlignTextView extends AppCompatTextView {

    private final String namespace = "rong.android.TextView";
    private String text;
    private float textSize;
    private int paddingLeft;
    private int paddingRight;
    private int paddingTop;
    private int paddingBottom;
    private float marginLeft;
    private float marginTop;
    private float marginRight;
    private float marginBottom;
    private int textColor;
    private JSONArray colorIndex;
    private Paint paint1 = new Paint();
    private Paint paintColor = new Paint();
    private float textShowWidth;
    private float Spacing = 0;
    private float LineSpacing = 1.3f;//行與行的間距

    public CustomAlignTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        text = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "text");
        textSize = attrs.getAttributeIntValue(namespace, "textSize", 25);//字體大小
        textColor = attrs.getAttributeIntValue(namespace, "textColor", Color.BLACK);//字體顏色
        paddingLeft = attrs.getAttributeIntValue(namespace, "paddingLeft", 0);
        paddingRight = attrs.getAttributeIntValue(namespace, "paddingRight", 0);
        paddingTop = attrs.getAttributeIntValue(namespace, "paddingTop", 0);
        paddingBottom = attrs.getAttributeIntValue(namespace, "paddingBottom", 0);
        marginLeft = attrs.getAttributeIntValue(namespace, "marginLeft", 0);
        marginRight = attrs.getAttributeIntValue(namespace, "marginRight", 0);
        marginTop = attrs.getAttributeIntValue(namespace, "marginTop", 0);
        marginBottom = attrs.getAttributeIntValue(namespace, "marginBottom", 0);
        paint1.setTextSize(textSize);
        paint1.setColor(textColor);
        paint1.setAntiAlias(true);
        paintColor.setAntiAlias(true);
        paintColor.setTextSize(textSize);
        paintColor.setColor(Color.BLACK);
    }

    public CustomAlignTextView(Context context, float textSize, int textColor, int paddingLeft, int paddingRight, float marginLeft, float marginRight) {
        super(context);
        this.textSize = textSize;
        this.textColor = textColor;
        this.paddingLeft = paddingLeft;
        this.paddingRight = paddingRight;
        this.marginLeft = marginLeft;
        this.marginRight = marginRight;
        paint1.setTextSize(textSize);
        paint1.setColor(textColor);
        paint1.setAntiAlias(true);
        paintColor.setAntiAlias(true);
        paintColor.setTextSize(textSize);
        paintColor.setColor(Color.BLACK);
    }


    public JSONArray getColorIndex() {
        return colorIndex;
    }

    public void setColorIndex(JSONArray colorIndex) {
        this.colorIndex = colorIndex;
    }

    /**
     * 傳入一個索引,判斷當(dāng)前字是否被高亮
     *
     * @param index
     * @return
     * @throws JSONException
     */
    public boolean isColor(int index) throws JSONException {
        if (colorIndex == null) {
            return false;
        }
        for (int i = 0; i < colorIndex.length(); i++) {
            JSONArray array = colorIndex.getJSONArray(i);
            int start = array.getInt(0);
            int end = array.getInt(1) - 1;
            if (index >= start && index <= end) {
                return true;
            }
        }
        return false;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        //      super.onDraw(canvas);
        View view = (View) this.getParent();
        textShowWidth = view.getMeasuredWidth() - paddingLeft - paddingRight - marginLeft - marginRight;
        int lineCount = 0;

        text = this.getText().toString();//.replaceAll("\n", "\r\n");
        char[] textCharArray = text.toCharArray();
        // 已繪的寬度
        float drawedWidth = 0;
        float charWidth;
        for (int i = 0; i < textCharArray.length; i++) {
            charWidth = paint1.measureText(textCharArray, i, 1);

            if (textCharArray[i] == '\n') {
                lineCount++;
                drawedWidth = 0;
                continue;
            }
            if (textShowWidth - drawedWidth < charWidth) {
                lineCount++;
                drawedWidth = 0;
            }
            boolean color = false;
            try {
                color = isColor(i);
            } catch (JSONException e1) {
                e1.printStackTrace();
            }

            if (color) {

                canvas.drawText(textCharArray, i, 1, paddingLeft + drawedWidth, (lineCount + 1) * textSize * LineSpacing, paintColor);
            } else {

                canvas.drawText(textCharArray, i, 1, paddingLeft + drawedWidth, (lineCount + 1) * textSize * LineSpacing, paint1);
            }

            if (textCharArray[i] > 127 && textCharArray[i] != '躲庄、' && textCharArray[i] != '查剖,' && textCharArray[i] != '。' && textCharArray[i] != ':' && textCharArray[i] != '噪窘!') {

                drawedWidth += charWidth + Spacing;

            } else {
                drawedWidth += charWidth;

            }
        }
        setHeight((int) ((lineCount + 1) * (int) textSize * LineSpacing + 10));
    }

    public void setAlignTextColor(int colorVal) {
        textColor = colorVal;
        paint1.setColor(textColor);
        paintColor.setColor(textColor);

        super.setTextColor(textColor);
    }

    public void setAlignPadding(int left, int right, int top, int bottom) {

        paddingLeft = left;
        paddingRight = right;
        paddingTop = top;
        paddingBottom = bottom;

        super.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
    }

    public void setAlignMargin(float left, float right, float top, float bottom) {

        marginLeft = left;
        marginRight = right;
        marginTop = top;
        marginBottom = bottom;

    }

    public void setAlignSpacing(float spacing) {
        Spacing = spacing;
    }

    public void setAlignLineSpacing(float lineSpacing) {
        LineSpacing = lineSpacing;
    }

    public void setAlignTextSize(float textSize) {
        this.textSize = textSize;
        paint1.setTextSize(textSize);
        paintColor.setTextSize(textSize);
        super.setTextSize(textSize);
    }

    public void setText(String text) {
        this.text = stringFilter(ToDBC(text));
        super.setText(this.text);
    }

    /**
     * 半角轉(zhuǎn)換為全角
     *
     * @param input
     * @return
     */
    public String ToDBC(String input) {
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 12288) {// 全角空格為12288笋庄,半角空格為32
                c[i] = (char) 32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375)// 其他字符半角(33-126)與全角(65281-65374)的對應(yīng)關(guān)系是:均相差65248
                c[i] = (char) (c[i] - 65248);
        }
        return new String(c);
    }

    /**
     * 去除特殊字符或?qū)⑺兄形臉?biāo)號替換為英文標(biāo)號
     *
     * @param str
     * @return
     */
    public String stringFilter(String str) {
        str = str.replaceAll("【", "[").replaceAll("】", "]").replaceAll("!", "!").replaceAll(":", ":");// 替換中文標(biāo)號
        String regEx = "[『』]"; // 清除掉特殊字符
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }
}

3.主activity:

package com.example.lenovo.mytest1708;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import com.example.lenovo.mytest1708.view.CustomAlignTextView;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        TextView tv = (TextView) findViewById(R.id.text);
        CustomAlignTextView text = (CustomAlignTextView) findViewById(R.id.text001);

        String str = "恭喜您成功邀請到測試一森(4544587979182)加入海洋大家庭,快帶小伙伴開啟創(chuàng)業(yè)之旅吧直砂!恭喜您成功邀請到測試一森(4544587979182)加入海洋大家庭菌仁,快帶小伙伴開啟創(chuàng)業(yè)之旅吧!恭喜您成功邀請到測試一森(4544587979182)加入海洋大家庭静暂,快帶小伙伴開啟創(chuàng)業(yè)之旅吧济丘!";

        text.setAlignTextColor(getResources().getColor(R.color.colorAccent));
        text.setAlignTextSize(sp2px(this, 15));
        //top,buttom暫時無效, 可通過view或space來設(shè)置,歡迎高手來指導(dǎo)
        text.setAlignPadding(dip2px(this, 20), dip2px(this, 20), 0, 0);

        tv.setText(str);
        text.setText(str);

    }

    /**
     * 將sp值轉(zhuǎn)換為px值,保證文字大小不變
     *
     * @param spValue
     * @return
     */
    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    /**
     * 將px值轉(zhuǎn)換為sp值洽蛀,保證文字大小不變
     *
     * @param pxValue
     * @return
     */

    public int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    /**
     * 根據(jù)手機的分辨率從 dp 的單位 轉(zhuǎn)成為 px(像素)
     */
    public int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根據(jù)手機的分辨率從 px(像素) 的單位 轉(zhuǎn)成為 dp
     */
    public int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}

可能達(dá)不到某些場景的理想效果, 但是有借鑒的地方, 還請各位大神多多賜教...

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末摹迷,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子郊供,更是在濱河造成了極大的恐慌峡碉,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,122評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件颂碘,死亡現(xiàn)場離奇詭異异赫,居然都是意外死亡,警方通過查閱死者的電腦和手機头岔,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評論 3 395
  • 文/潘曉璐 我一進(jìn)店門塔拳,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人峡竣,你說我怎么就攤上這事靠抑。” “怎么了适掰?”我有些...
    開封第一講書人閱讀 164,491評論 0 354
  • 文/不壞的土叔 我叫張陵颂碧,是天一觀的道長。 經(jīng)常有香客問我类浪,道長载城,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,636評論 1 293
  • 正文 為了忘掉前任费就,我火速辦了婚禮诉瓦,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘力细。我一直安慰自己睬澡,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,676評論 6 392
  • 文/花漫 我一把揭開白布眠蚂。 她就那樣靜靜地躺著煞聪,像睡著了一般。 火紅的嫁衣襯著肌膚如雪逝慧。 梳的紋絲不亂的頭發(fā)上昔脯,一...
    開封第一講書人閱讀 51,541評論 1 305
  • 那天啄糙,我揣著相機與錄音,去河邊找鬼云稚。 笑死迈套,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的碱鳞。 我是一名探鬼主播,決...
    沈念sama閱讀 40,292評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼踱蛀,長吁一口氣:“原來是場噩夢啊……” “哼窿给!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起率拒,我...
    開封第一講書人閱讀 39,211評論 0 276
  • 序言:老撾萬榮一對情侶失蹤崩泡,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后猬膨,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體角撞,經(jīng)...
    沈念sama閱讀 45,655評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,846評論 3 336
  • 正文 我和宋清朗相戀三年勃痴,在試婚紗的時候發(fā)現(xiàn)自己被綠了谒所。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,965評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡沛申,死狀恐怖劣领,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情铁材,我是刑警寧澤尖淘,帶...
    沈念sama閱讀 35,684評論 5 347
  • 正文 年R本政府宣布,位于F島的核電站著觉,受9級特大地震影響村生,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜饼丘,卻給世界環(huán)境...
    茶點故事閱讀 41,295評論 3 329
  • 文/蒙蒙 一趁桃、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧葬毫,春花似錦镇辉、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至烂斋,卻和暖如春屹逛,著一層夾襖步出監(jiān)牢的瞬間础废,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評論 1 269
  • 我被黑心中介騙來泰國打工罕模, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留评腺,地道東北人。 一個月前我還...
    沈念sama閱讀 48,126評論 3 370
  • 正文 我出身青樓淑掌,卻偏偏與公主長得像蒿讥,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子抛腕,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,914評論 2 355

推薦閱讀更多精彩內(nèi)容