本文实例讲述了Android编程中TextView字体属性设置方法(大小、字体、下划线、背景色)。分享给大家供大家参考,具体如下:
import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.BulletSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.ScaleXSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.SubscriptSpan;
import android.text.style.SuperscriptSpan;
import android.text.style.TypefaceSpan;
import android.text.style.UnderlineSpan;
public class TextViewUtil {
public SpannableString getSizeSpanUsePx(Context context, String str, int start, int end, int pxSize) {
SpannableString ss = new SpannableString(str);
ss.setSpan(new AbsoluteSizeSpan(pxSize), 4, str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return ss;
}
public SpannableString getSizeSpanUseDip(Context context, String str, int start, int end, int dipSize) {
SpannableString ss = new SpannableString(str);
ss.setSpan(new AbsoluteSizeSpan(dipSize, true), 4, str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return ss;
}
public SpannableString getSizeSpanSpToPx(Context context, String str, int start, int end, int spSize) {
SpannableString ss = new SpannableString(str);
ss.setSpan(new AbsoluteSizeSpan(DisplayUtil.sp2px(context, spSize)), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return ss;
}
public SpannableString getRelativeSizeSpan(Context context, String str, int start, int end, float relativeSize) {
SpannableString ss = new SpannableString(str);
ss.setSpan(new RelativeSizeSpan(relativeSize), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return ss;
}
public SpannableString getTypeFaceSpan(Context context, String str, int start, int end, String typeface) {
SpannableString ss = new SpannableString(str);
ss.setSpan(new TypefaceSpan(typeface), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return ss;
}
public SpannableString getStyleSpan(Context context, String str, int start, int end, int style) {
SpannableString ss = new SpannableString(str);
ss.setSpan(new StyleSpan(style), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return ss;
}
public SpannableString getUnderLineSpan(Context context, String str, int start, int end) {
SpannableString ss = new SpannableString(str);
ss.setSpan(new UnderlineSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return ss;
}
public SpannableString getDeleteLineSpan(Context context, String str, int start, int end) {
SpannableString ss = new SpannableString(str);
ss.setSpan(new StrikethroughSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return ss;
}
public SpannableString getSuperscriptSpan(Context context, String str, int start, int end) {
SpannableString ss = new SpannableString(str);
ss.setSpan(new SuperscriptSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return ss;
}
public SpannableString getScaleSpan(Context context, String str, int start, int end, float scale) {
SpannableString ss = new SpannableString(str);
ss.setSpan(new ScaleXSpan(scale), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return ss;
}
public SpannableString getSubscriptSpan(Context context, String str, int start, int end) {
SpannableString ss = new SpannableString(str);
ss.setSpan(new SubscriptSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return ss;
}
public SpannableString getBackGroundColorSpan(Context context, String str, int start, int end, int color) {
SpannableString ss = new SpannableString(str);
ss.setSpan(new BackgroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return ss;
}
public SpannableString getBackGroundColorSpan(Context context, String str, int start, int end, String color) {
SpannableString ss = new SpannableString(str);
ss.setSpan(new BackgroundColorSpan(Color.parseColor(color)), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return ss;
}
public SpannableString getForegroundColorSpan(Context context, String str, int start, int end, int color) {
SpannableString ss = new SpannableString(str);
ss.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return ss;
}
public SpannableString getForegroundColorSpan(Context context, String str, int start, int end, String color) {
SpannableString ss = new SpannableString(str);
ss.setSpan(new ForegroundColorSpan(Color.parseColor(color)), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return ss;
}
}
希望本文所述对大家Android程序设计有所帮助。
您可能感兴趣的文章:Android编程实现自动调整TextView字体大小以适应文字长度的方法android根据分辨率自动调整字体大小的实例代码Android TextView 设置字体大小的方法Android中App字体大小不随系统改变而改变Android Studio设置主题与字体大小图文教程Android字体大小自适应不同分辨率的解决办法Android 自定义TextView实现文本内容自动调整字体大小Android Studio如何修改字体的大小浅扒Android动态设置字体大小的示例Android如何动态调整应用字体大小详解