背景
原生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á)不到某些場景的理想效果, 但是有借鑒的地方, 還請各位大神多多賜教...