IT_Programming/Android_Java

[펌] Android - Spannable 을 잘 사용하면 TextView 의 마스터!!

JJun ™ 2014. 11. 25. 01:01



 출처: http://aroundck.tistory.com/2727


BulletSpan


/*

public BulletSpan (int gapWidth, int color)

-gapWidth: gap in px between bullet and text

-color: bullet color (optionnal, default is transparent)

*/


//create a black BulletSpan with a gap of 15px

span = new BulletSpan(15, Color.BLACK);






QuoteSpan


/*

public QuoteSpan (int color)

-color: quote vertical line color (optionnal, default is Color.BLUE)

*/


//create a red quote

span = new QuoteSpan(Color.RED);






AlignmentSpan.Standard


/*

public Standard(Layout.Alignment align)

-align: alignment to set

*/


//align center a paragraph

span = new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER);






UnderlineSpan


//underline a character

span = new UnderlineSpan();









StrikethroughSpan


//strikethrough a character

span = new StrikethroughSpan();






SubscriptSpan


//subscript a character

span = new SubscriptSpan();






SuperscriptSpan


//superscript a character

span = new SuperscriptSpan();






BackgroundColorSpan


/*

public BackgroundColorSpan (int color)

-color: background color

*/


//set a green background

span = new BackgroundColorSpan(Color.GREEN);






ForegroundColorSpan


/*

public ForegroundColorSpan (int color)

-color: foreground color

*/


//set a red foreground

span = new ForegroundColorSpan(Color.RED);









ImageSpan


//replace a character by pic1_small image

span = new ImageSpan(this, R.drawable.pic1_small);






StyleSpan


/*

public StyleSpan (int style)

-style: int describing the style (android.graphics.Typeface)

*/


//set a bold+italic style

span = new StyleSpan(Typeface.BOLD | Typeface.ITALIC);






TypefaceSpan


/*

public TypefaceSpan (String family)

-family: a font family

*/


//set the serif family

span = new TypefaceSpan("serif");






TextApperanceSpan


/*

public  TextAppearanceSpan(Context context, int appearance, int colorList)

-context: a valid context

-appearance: text appearance resource (ex: android.R.style.TextAppearance_Small)

-colorList: a text color resource (ex: android.R.styleable.Theme_textColorPrimary)


public TextAppearanceSpan(String family, int style, int size, ColorStateList color, ColorStateList linkColor)

-family: a font family

-style: int describing the style (android.graphics.Typeface)

-size: text size

-color: a text color

-linkColor: a link text color

*/


//set the serif family

span = new TextAppearanceSpan(this/*a context*/, R.style.SpecialTextAppearance);


<style name="SpecialTextAppearance" parent="@android:style/TextAppearance">

    <item name="android:textColor">@color/color1</item>

    <item name="android:textColorHighlight">@color/color2</item>

    <item name="android:textColorHint">@color/color3</item>

    <item name="android:textColorLink">@color/color4</item>

    <item name="android:textSize">28sp</item>

    <item name="android:textStyle">italic</item>

</style>






AbsoluteSizeSpan


/*

public AbsoluteSizeSpan(int size, boolean dip)

-size: a size

-dip: false, size is in px; true, size is in dip (optionnal, default false)

*/


//set text size to 24dp

span = new AbsoluteSizeSpan(24, true);






RelativeSizeSpan


/*

public RelativeSizeSpan(float proportion)

-proportion: a proportion of the actual text size

*/


//set text size 2 times bigger 

span = new RelativeSizeSpan(2.0f);






ScaleXSpan


/*

public ScaleXSpan(float proportion)

-proportion: a proportion of actual text scale x

*/


//scale x 3 times bigger 

span = new ScaleXSpan(3.0f);









MaskFilterSpan


/*

public MaskFilterSpan(MaskFilter filter)

-filter: a filter to apply

*/


//Blur a character

span = new MaskFilterSpan(new BlurMaskFilter(density*2, BlurMaskFilter.Blur.NORMAL));

//Emboss a character

span = new MaskFilterSpan(new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f));