今天忽然发现android项目中的文字排版参差不齐,不得不想办法解决一下。经过搜索后网上很多都说半角字符与全角字符混乱所致这个的解决方法是:将textview中的字符全角化。即将所有的数字、字母及标点全部转为全角字符,使它们与汉字同占两个字节,这样就可以避免由于占位导致的排版混乱问题了。 半角转为全角的代码如下,只需调用即可
/**
* 半角转换为全角
*
* @param input
* @return
*/
publicstaticString ToDBC(String input) {
char[] c = input.toCharArray();
for(inti =0; i < c.length; i++) {
if(c[i] ==12288) {
c[i] = (char)32;
continue;
}
if(c[i] >65280&& c[i] <65375)
c[i] = (char) (c[i] -65248);
}
returnnewString(c);
}
2. 去除特殊字符或将所有中文标号替换为英文标号。利用正则表达式将所有特殊字符过滤,或利用replaceAll()将中文标号替换为英文标号。
则转化之后,则可解决排版混乱问题。
/**
* 去除特殊字符或将所有中文标号替换为英文标号
*
* @param str
* @return
*/
publicstaticString stringFilter(String str) {
str = str.replaceAll("【","[").replaceAll("】","]")
.replaceAll("!","!").replaceAll(":",":");// 替换中文标号
String regEx ="[『』]";// 清除掉特殊字符
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
returnm.replaceAll("").trim();
}
然而都不能解决问题 我只能自定义TextView了
public class MyTextViewextends AppCompatTextView {
private Stringtext;
private float textSize;
private float paddingLeft;
private float paddingRight;
private float marginLeft;
private float marginRight;
private float marginTop;
private float marginBottom;
private int textColor;
//字体上下间距
private int fontSize;
private Paintpaint1 =new Paint();
private float textShowWidth;
public MyTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context,attrs);
}
private void init(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyTextView);
try {
textSize =15;
textColor = Color.WHITE;
fontSize = dp2px(context,3);
if (a !=null){
textSize = a.getDimension(R.styleable.MyTextView_mTextSize,sp2px(context,textSize));
paddingLeft = a.getDimension(R.styleable.MyTextView_mPaddingLeft,paddingLeft);
paddingRight = a.getDimension(R.styleable.MyTextView_mPaddingRight,paddingRight);
marginLeft = a.getDimension(R.styleable.MyTextView_mMarginLeft,marginLeft);
marginRight = a.getDimension(R.styleable.MyTextView_mPaddingRight,marginRight);
marginTop = a.getDimension(R.styleable.MyTextView_mMarginTop,marginTop);
marginBottom = a.getDimension(R.styleable.MyTextView_mMarginBottom,marginBottom);
textColor = a.getInt(R.styleable.MyTextView_mTextColor,textColor);
paint1.setColor(textColor);
//设置字体加粗
paint1.setTextSize(textSize);
paint1.setFakeBoldText(true);
paint1.setAntiAlias(true);
textShowWidth = ((Activity) context).getWindowManager()
.getDefaultDisplay().getWidth() -paddingLeft -paddingRight -marginLeft -marginRight;
}
}finally {
a.recycle();
}
}
@Override
protected void onDraw(Canvas canvas) {
int lineCount =0;
text =this.getText().toString();
if (text ==null){
return;
}
char[] textCharArray =text.toCharArray();
float drawWidth =0;
float charWidth;
for (int i =0;i < textCharArray.length;i++){
charWidth =paint1.measureText(textCharArray,i,1);
if (textCharArray[i] =='\n'){
lineCount ++;
drawWidth =0;
continue;
}
if (textShowWidth - drawWidth < charWidth) {
lineCount++;
drawWidth =0;
}
canvas.drawText(textCharArray, i, 1, paddingLeft + drawWidth,
(lineCount +1) *textSize +marginTop+fontSize*lineCount, paint1);
drawWidth += charWidth;
}
setHeight( ((lineCount +1) * (int)textSize + (int)marginTop+5+fontSize*lineCount+ (int)marginBottom));
}
private int sp2px(Context context, float sp){
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,sp,context.getResources().getDisplayMetrics());
}
private int dp2px(Context context,float dp){
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dp,context.getResources().getDisplayMetrics());
}
}
命名啥的不要在意了 大致就是这么解决的