I got problem when I try to set image and text vertically center. So I'm using SpannableStringBuilder and ImageSpan. I try to combile image and text by this code :
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
Drawable drawableIcon = null;
// space added before text for convenience
SpannableStringBuilder sb = new SpannableStringBuilder(" ");
ImageSpan span = null;
switch (position) {
case 0:
drawableIcon = getResources().getDrawable(R.drawable.ic_action_unread);
drawableIcon.setBounds(0, 0, drawableIcon.getIntrinsicWidth(), drawableIcon.getIntrinsicHeight());
span = new ImageSpan(drawableIcon, ImageSpan.ALIGN_BASELINE);
sb.append(getString(R.string.title_task_fragment).toUpperCase(l));
sb.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//return getString(R.string.title_task_fragment).toUpperCase(l);
return sb;
case 1:
drawableIcon = getResources().getDrawable(R.drawable.ic_action_camera);
drawableIcon.setBounds(0, 0, drawableIcon.getIntrinsicWidth(), drawableIcon.getIntrinsicHeight());
span = new ImageSpan(drawableIcon, ImageSpan.ALIGN_BASELINE);
sb.append(getString(R.string.title_news_fragment).toUpperCase(l));
sb.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//return getString(R.string.title_news_fragment).toUpperCase(l);
return sb;
case 2:
drawableIcon = getResources().getDrawable(R.drawable.ic_action_settings_white);
drawableIcon.setBounds(0, 0, drawableIcon.getIntrinsicWidth(), drawableIcon.getIntrinsicHeight());
span = new ImageSpan(drawableIcon, ImageSpan.ALIGN_BASELINE);
sb.append(getString(R.string.title_settings_fragment).toUpperCase(l));
sb.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//return getString(R.string.title_settings_fragment).toUpperCase(l);
return sb;
}
return null;
}
And this is my xml layout which contains PagerTabStrip
<android.support.v4.view.ViewPager android:id="@+id/container"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".WrapperMainActivity" >
<android.support.v4.view.PagerTabStrip
android:id="@+id/pager_tab_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="@color/title_strip_background1"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>
I've already set the ImageSpan.ALIGN_BASELINE, but I'm not get the expected result. The text remains at the bottom, not aligned in the same line of the image icon...
Have I missed something?
Thanks...