Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

android: How to add icons/drawables to the PagerTabStrip from the Android Support Lib version 4?

android: How to add icons/drawables to the PagerTabStrip from the Android Support Lib version 4 ?

This is very specific question to people aware of the PagerTabStrip, I couldn't find enough examples anywhere, it's somehow new (The PagerTabStrip) so i couldn't find enough info.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can easily add an icon/drawable to the PageTabStrip using SpannableString or SpannableStringBuilder.

For example, to display an icon before the text :

Drawable myDrawable; //Drawable you want to display

@Override
public CharSequence getPageTitle(int position) {

    SpannableStringBuilder sb = new SpannableStringBuilder(" Page #"+ position); // space added before text for convenience

    myDrawable.setBounds(0, 0, myDrawable.getIntrinsicWidth(), myDrawable.getIntrinsicHeight()); 
    ImageSpan span = new ImageSpan(myDrawable, ImageSpan.ALIGN_BASELINE); 
    sb.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 

    return sb;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...