由於項目需要文本字數爲2-3-4個,需要進行對齊處理,修改了textView的setText()實現簡單的對齊效果。
package com.example.toasttest;
import android.content.Context;import android.util.AttributeSet;
import android.widget.TextView;
public class HaveBlanTextView extends TextView {
public HaveBlanTextView(Context context) {
super(context);
}
public HaveBlanTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public HaveBlanTextView(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public void setText(CharSequence text, BufferType type) {
// 一個漢字等於4個英文空格
if (text.length() == 2) {
String t1 = text.toString().substring(0, 1);
String t2 = text.toString().substring(1, 2);
String blan = " ";
text = (CharSequence) (t1 + blan + t2);
} else if (text.length() == 3) {
String t1 = text.toString().substring(0, 1);
String t2 = text.toString().substring(1, 2);
String t3 = text.toString().substring(2, 3);
String blan = " ";
text = (CharSequence) (t1 + blan + t2 + blan + t3);
}
super.setText(text, type);
}
}