Android TextView默认是标点符号不能再行尾和行首,英文单词不能拆分;
1、网上有说修改源码(表示我是菜鸟)、也有说转半角或全角(这种真的不太行啊,半角的话还是有点问题;全角的话英文就难看了,显示出来只能呵呵~~。)
2、自定义(我是还是很赞同的),下面是我看了网上别人的自定义代码写出来的(不喜勿喷),先上代码:
/**
* Created by wmi01 on 2015/11/23.
* 文字排版自动换行
*/
public class ComposeTextView extends View {
// 总高度、宽度
private int sumHeight = 0;
private int maxWidth = 0;
// 一些属性
private int textColor = getResources().getColor(android.R.color.black);
private int textSize = 14;
private int lineSpace = 5; //行间距
private int typeFace = 0;
private String text = "";
private int maxLine = Integer.MAX_VALUE; //最大行数
// 上下左右的距离
private int left_Margin = 10;
private int right_Margin = 10;
private int top_Margin = 10;
private int bottom_Margin = 10;
private Paint mPaint;
public ComposeTextView(Context context) {
super(context);
}
public ComposeTextView(Context context, AttributeSet attrs) {
super(context, attrs, 0);
DisplayMetrics dm = getResources().getDisplayMetrics();
//为属性定义单位
textSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, textSize, dm);
lineSpace = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, lineSpace, dm);
left_Margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, left_Margin, dm);
right_Margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, right_Margin, dm);
top_Margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, top_Margin, dm);
bottom_Margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, bottom_Margin, dm);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ComposeTextView);
textSize = a.getDimensionPixelSize(R.styleable.ComposeTextView_textSize, textSize);
textColor = a.getColor(R.styleable.ComposeTextView_textColor, textColor);
lineSpace = a.getDimensionPixelSize(R.styleable.ComposeTextView_lineSpace, lineSpace);
maxLine = a.getInt(R.styleable.ComposeTextView_maxLine, maxLine);
typeFace = a.getInt(R.styleable.ComposeTextView_typeFace, typeFace);
left_Margin = a.getDimensionPixelSize(R.styleable.ComposeTextView_left_Margin, left_Margin);
right_Margin = a.getDimensionPixelSize(R.styleable.ComposeTextView_right_Margin, right_Margin);
top_Margin = a.getDimensionPixelSize(R.styleable.ComposeTextView_top_Margin, top_Margin);
bottom_Margin = a.getDimensionPixelSize(R.styleable.ComposeTextView_bottom_Margin, bottom_Margin);
a.recycle();
int width = dm.widthPixels;
maxWidth = width - left_Margin - right_Margin;
mPaint = new Paint();