Android原⽣的TextView,遇到中⽂和数字或者中⽂和英⽂⼀起混排的时候,会出现⾃动换⾏的现象。本来第⼀⾏还有很多空间可以显⽰,却莫名其妙地换⾏显⽰。
解决思路采⽤的是⾃定义view,挨个画字符,⼀个⼀个吧⽂字画出来
package _ams.view.widget;
t.Context;
t.res.TypedArray;
aphics.Canvas;
aphics.Color;
aphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import _ams.R;
public class MyTextView extends View {
//内容填充画笔
private Paint contentPaint;
//标准的字体颜⾊
private String contentNormalColor = "#737373";
//有焦点的字体颜⾊
private String contentFocuedColor = "#333333";
//控件宽度
private int viewWidth = 0;
//控件⾼度
private int viewHeight = 0;
//标准的字的样式
public static final int TEXT_TYPE_NORMAL = 1;
//控件获取焦点的时候进⾏的处理
public static final int TEXT_TYPE_FOCUED = 2;
//默认是标准的样式
private int currentTextType = TEXT_TYPE_NORMAL;
//默认当前的颜⾊
private String textColor = "#333333";
//字体⼤⼩
private int textSize = 40;
/
/内容
private String mText = "测试的⽂字信息";
//最⼩view⾼度
private float minHeight = 0;
//最⼩view宽度
private float minWidth = 0;
//⾏间距
private float lineSpace;
//最⼤⾏数
private int maxLines = 0;
//⽂字测量⼯具
private Paint.FontMetricsInt textFm;
///真实的⾏数
private int realLines;
//当前显⽰的⾏数
private int line;
//在末尾是否显⽰省略号
private boolean showEllipsise;
//⽂字显⽰区的宽度
private int textWidth;
//单⾏⽂字的⾼度
private int signleLineHeight;
private int mPaddingLeft,mPaddingRight,mPaddingTop,mPaddingBottom;
/**
* 省略号
**/
private String ellipsis = "...";
public MyTextView(Context context) {
this(context,null);
}
public MyTextView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAttr(context,attrs);
init();
}
private boolean isFristload;
/**
* 初始化
*/
private void init() {
contentPaint = new Paint();
contentPaint.setTextSize(textSize);
contentPaint.setAntiAlias(true);
contentPaint.setStrokeWidth(2);
contentPaint.setColor(Color.parseColor(textColor));
contentPaint.setTextAlign(Paint.Align.LEFT);
textFm = FontMetricsInt();
signleLineHeight=Math.p-textFm.bottom);
}
/**
* 初始化属性
* @param context
* @param attrs
*/
private void initAttr(Context context,AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTextView);
mPaddingLeft = DimensionPixelSize(R.styleable.MyTextView_paddingLeft, 0);
mPaddingRight = DimensionPixelSize(R.styleable.MyTextView_paddingRight, 0);
mPaddingTop = DimensionPixelSize(R.styleable.MyTextView_paddingTop, 0);
mPaddingBottom = DimensionPixelSize(R.styleable.MyTextView_paddingBottom, 0);
mText = String(R.styleable.MyTextView_text);
textColor = String(R.styleable.MyTextView_textColo);
maxLines = Integer(R.styleable.MyTextView_maxLine,-1);
if(textColor==null){
textColor="#000000";
}
textSize = DimensionPixelSize(R.styleable.MyTextView_textSize, 50);
lineSpace = Integer(R.styleable.MyTextView_lineSpacing, 0);
}
public void setText(String ss){
this.mText=ss;
invalidate();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
viewWidth=getMeasuredWidth();
textWidth=viewWidth-mPaddingLeft-mPaddingRight;
viewHeight= (int) getViewHeight();
setMeasuredDimension(viewWidth, viewHeight);
}
private float getViewHeight() {
char[] CharArray();
float ww=0.0f;
int count=0;
StringBuilder sb=new StringBuilder();
for(int i=0;i<textChars.length;i++){
float v = asureText(textChars[i] + "");
if(ww+v<=textWidth){
sb.append(textChars[i]);
ww+=v;
}
else{
if (maxLines < 0 ) {
count++;
sb=new StringBuilder();
ww=0.0f;
ww+=v;
sb.append(textChars[i]);
} else {
if (maxLines <= count) {
break;
} else {
count++;
sb=new StringBuilder();
ww=0.0f;
ww+=v;
sb.append(textChars[i]);
}
}
}
}
if (maxLines < 0) {
if (sb.toString().length() != 0) {
count++;
}
} else {
if (maxLines <= count) {
} else {
count++;
}
}
return count*signleLineHeight+lineSpace*(count-1)+mPaddingBottom+mPaddingTop; }
@Override
protected void onDraw(Canvas canvas) {
drawText(canvas);
}
/**
* 循环遍历画⽂字
* @param canvas
*/
private void drawText(Canvas canvas) {
char[] CharArray();
float ww=0.0f;
float startL=0.0f;
float startT=0.0f;
startL+=mPaddingLeft;
startT+=mPaddingTop+signleLineHeight/2+ (p)/2 - textFm.bottom; StringBuilder sb=new StringBuilder();
for(int i=0;i<textChars.length;i++){
float v = asureText(textChars[i] + "");
if(ww+v<=textWidth){
sb.append(textChars[i]);
ww+=v;
}
else{
canvas.String(),startL,startT,contentPaint);
startT+=signleLineHeight+lineSpace;
sb=new StringBuilder();
ww=0.0f;
ww+=v;
sb.append(textChars[i]);
line++;
if (maxLines > 0 && maxLines <= line) {
break;
}
}
}
if (maxLines > 0 && maxLines <= line) {
}else {
String().length()>0){
canvas.String(),startL,startT,contentPaint);
}
}
}
}
⾃定义属性
<declare-styleable name="MyTextView">
<attr name="paddingLeft" format="dimension" />
<attr name="paddingTop" format="dimension" />
<attr name="paddingRight" format="dimension" />
recycle是什么意思<attr name="paddingBottom" format="dimension" />
<attr name="textSize" format="dimension" />
<attr name="textColor" format="string" />
<attr name="text" format="string"/>
<attr name="lineSpacing" format="integer" />
<attr name="maxLine" format="integer"/>
<attr name="ellipsis" format="boolean" />
</declare-styleable>
⽬前⼀些⾃定义属性没有添加,后期完善
发布评论