我有问题,当我尝试设置图片和文字垂直居中。 所以我使用SpannableStringBuilder和ImageSpan。 我试图通过这个代码combile图片和文字:
@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;
}
这是一个包含PagerTabStrip我的XML布局
<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>
我已经设定的ImageSpan.ALIGN_BASELINE,但我没有得到预期的结果。 文本保持在底部,在同一条直线上的图像图标的不对齐...
我错过了什么?
谢谢...