This article describes the TextView Font property setting method (size, font, underline, background color) in Android programming. Share to everyone for your reference, specific as follows:
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 {/** * Set font size, with px * * @param context * * @param str * Target String * @param Start * Starting position * @param end * @param pxsize * Pixel size * @return/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; /** * Set font size, with dip * * @param context * * @param str * Target String * @param start * Starting position * Param end * @param dipsize * Pixel size * @return/Public spannablestring Getsizespanusedip (Contex
T 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; /** * Set font size, with SP * * @param context * * @param str * Target String * @param start * Starting position * @p Aram End * @param spsize * SP size * @return/public spannablestring GETSIZESPANSPTOPX
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;
/** * Set Font relative size * * @param context * @param str * Target String * @param start * @param end * End Position * @param relativesize * Relative size such as: 0.5f,2.0f * @return/public spannablestring getrelativesizes Pan (context, String str, int start, int end, float relativesize) {spannablestring ss = new Spannablestring (St
R);
Ss.setspan (New Relativesizespan (RelativeSize), start, end, spanned.span_exclusive_exclusive);
return SS;
/** * Set Font * * @param context * @param str * Target String * @param start * @param end * End Position * @param typeface * font type such as: Default,efault-bold,monospace,serif,sans-serif * @return/public Spa Nnablestring 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;
/** * Set Font shape * * @param context * @param str * Target String * @param start * @param end * End Position * @param style * font type such as: Typeface.normal normal typeface.bold bold typeface.italic italic * Typeface.bold_itali C Bold Italic * @return/Public spannablestring Getstylespan (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;
/** * Set Font underline * * @param context * @param str * Target String * @param start * @param end * End Position * @return/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; }/** * SetFont deletion line * * @param context * @param str * Target String * @param start * @param end * Ending position * @return */Public spannablestring Getdeletelinespan (context, String str, int start, int end) {Spannab
lestring ss = new spannablestring (str);
Ss.setspan (New Strikethroughspan (), Start, end, spanned.span_exclusive_exclusive);
return SS;
/** * Set Superscript * * @param context * @param str * Target String * @param start * @param end
* End Position * @return * * spannablestring getsuperscriptspan (context, String str, int start, int end) {
spannablestring ss = new spannablestring (str);
Ss.setspan (New Superscriptspan (), Start, end, spanned.span_exclusive_exclusive);
return SS;
/** * Set magnification factor * * @param context * @param str * Target String * @param start * @param end * End Position * @param scale * magnification, x-axis, y-axis invariant such as: 0.5f, 2.0f * @return * * Public Spannablestring Getscalespan (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;
/** * Set Subscript * * @param context * @param str * Target String * @param start * @param end
* End Position * @return * * spannablestring getsubscriptspan (context, String str, int start, int end) {
spannablestring ss = new spannablestring (str);
Ss.setspan (New Subscriptspan (), Start, end, spanned.span_exclusive_exclusive);
return SS;
/** * Set Background color * * @param context * @param str * Target String * @param start * @param end * End Position * @param color * value such as Color.Black * @return/Public spannablestring Getbackgroundcolorspan (Con
Text 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; /** * Set Background color * * @param context * * @param str * Target String * @param start * Starting position * @param End * Ending position * @param color * value such as: #CCCCCC * @return/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; /** * Set Foreground color * @param context * * @param str * Target String * @param start * @param End * Ending position * @param color * value such as Color.Black * @return/public spannablestring GETFOREGROUNDCOLORSP
A (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; /** * Set Foreground color * @param context * * @param str * Target String * @param start * @param End * Ending position * @param color * value such as: #CCCCCC * @return/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;
}
}
I hope this article will help you with your Android programming.