public class CustomTypefaceSpan extends TypefaceSpan { private final Typeface newType;
public CustomTypefaceSpan(String family, Typeface type) { super(family);
newType = type;
}
@Override
public void updateDrawState(TextPaint ds) {
applyCustomTypeFace(ds, newType);
}
@Override
public void updateMeasureState(TextPaint paint) {
applyCustomTypeFace(paint, newType);
}
private static void applyCustomTypeFace(Paint paint, Typeface tf) { int oldStyle;
Typeface old = paint.getTypeface(); if (old == null) {
oldStyle = 0; } else {
oldStyle = old.getStyle();
}
int fake = oldStyle & ~tf.getStyle(); if ((fake & Typeface.BOLD) != 0) {
paint.setFakeBoldText(true); }
if ((fake & Typeface.ITALIC) != 0) { paint.setTextSkewX(-0.25f);
}
paint.setTypeface(tf);
}
}
加删除线
全部加删除线
String sampleText = "This is a test strike";
textView.setPaintFlags(tv.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);
textView.setText(sampleText);
自定义文字位置
TextView txtView = (TextView) findViewById(R.id.txtView);
SpannableString spannableString = new SpannableString("RM123.456");
spannableString.setSpan( new TopAlignSuperscriptSpan( (float)0.35 ), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE );
txtView.setText(spannableString);
TopAlignSuperscriptSpan.java
private class TopAlignSuperscriptSpan extends SuperscriptSpan {
//divide superscript by this number
protected int fontScale = 2;
//shift value, 0 to 1.0
protected float shiftPercentage = 0; //doesn't shift
TopAlignSuperscriptSpan() {}
//sets the shift percentage
TopAlignSuperscriptSpan( float shiftPercentage ) {
if( shiftPercentage > 0.0 && shiftPercentage < 1.0 )
this.shiftPercentage = shiftPercentage;
}
@Override
public void updateDrawState( TextPaint tp ) {
//original ascent
float ascent = tp.ascent(); //scale down the font
tp.setTextSize( tp.getTextSize() / fontScale ); //get the new font ascent
float newAscent = tp.getFontMetrics().ascent;
//move baseline to top of old font, then move down size of new font //adjust for errors with shift percentage
tp.baselineShift += ( ascent - ascent * shiftPercentage )
- (newAscent - newAscent * shiftPercentage );
}
@Override
public void updateMeasureState( TextPaint tp ) {
updateDrawState( tp );
}
}
文本设置图片
//方法一
Drawable drawable = tv.getResources().getDrawable(resId);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
tv.setCompoundDrawables(null, null, drawable, null);
//方法二
public static void setTvDrawable(TextView tv, String content, int resId, int type) {
ImageSpan span = new ImageSpan(tv.getContext().getResources().getDrawable(resId));
SpannableString spanStr = null;
if (0 == type) {
spanStr = new SpannableString(content);
} else if (1 == type) {
spanStr = new SpannableString("0" + content);
spanStr.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
} else if (2 == type) {
spanStr = new SpannableString(content + "0");
spanStr.setSpan(span, spanStr.length() - 1, spanStr.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
}
tv.setText(spanStr);
}
选中下滑线
/**
* TextView选中下滑线
*/
private void setTabSelected(TextView btnSelected) {
Drawable selectedDrawable = ContextCompat.getDrawable(activity, R.drawable.shape_nav_indicator);
int screenWidth = QMUIDisplayHelper.getRealScreenSize(activity)[0];
int right = screenWidth / 2;
selectedDrawable.setBounds(0, 0, right, QMUIDisplayHelper.dp2px(activity, 3));
btnSelected.setCompoundDrawables(null, null, null, selectedDrawable);
int size = ll_home.getChildCount();
for (int i = 0; i < size; i++) {
if (btnSelected.getId() != ll_home.getChildAt(i).getId()) {
ll_home.getChildAt(i).setSelected(false);
((TextView) ll_home.getChildAt(i)).setCompoundDrawables(null, null, null, null);
}
}
}