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

ⅰ亾dé卋堺 提交于 2019-11-27 00:04:34

问题


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.


回答1:


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;
}



回答2:


@MatthewHousser if the solution of SteveR doesn't work it may be because you set a fixed size in PagerTabStrip in your XML. This is what I had :

<android.support.v4.view.PagerTabStrip
        android:id="@+id/pager_tab_strip"
        android:layout_width="match_parent"
        android:layout_height="30dp">
</android.support.v4.view.PagerTabStrip>

This is what I have now :

<android.support.v4.view.PagerTabStrip
        android:id="@+id/pager_tab_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
</android.support.v4.view.PagerTabStrip>

The image wasn't displayed in the first case, and is displayed in the second case. Hope it will help.



来源:https://stackoverflow.com/questions/11839031/android-how-to-add-icons-drawables-to-the-pagertabstrip-from-the-android-suppor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!