如何在Android FragmentPagerAdapter中添加页面标题和图标 [英] How to add page title and icon in android FragmentPagerAdapter
本文介绍了如何在Android FragmentPagerAdapter中添加页面标题和图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在标题页标题中显示带有相应图标的标题.就像下面的图片一样,但是只能显示标题和缺少的图标.
I want to display title with respective icon in with header page title. like below image, however only able to display title and missing icon.
这是我的示例代码.
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Log.e("POSITION", "position=" + position);
Fragment fragment;
switch (position) {
case 0:
//Fragment1
case 1:
//Fragment2
case 2:
//Fragment3
case 3:
//Fragment4
case 4:
//Fragment5
}
return null;
}
@Override
public int getCount() {
return NUM_PAGES;
}
// using both these techniques but could not get icon in title
/*
* public int getIconResId(int index) { return ICONS[index % ICONS.length]; }
*/
public int getPageIconResId(int position) {
return ICONS[position];
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
case 3:
return getString(R.string.title_section4).toUpperCase(l);
case 4:
return getString(R.string.title_section5).toUpperCase(l);
}
return null;
}
}
和onCreate中:
and in onCreate:
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
将此XML文件用于片段中的显示片段页面适配器;
Using this xml file for display fragment page adapter in fragment;
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/rlv2"
android:layout_marginTop="-30dp" >
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="@color/black"
android:textStyle="bold" />
</android.support.v4.view.ViewPager>
输出需要我的应用程序的图像.请问如何显示带有图标和标题的部分页面适配器.
Output image that i need my app.Could you please any idea how to display section page adapter with icon and title.
推荐答案
I got solution from Stever.
Drawable myDrawable;
String title;
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
myDrawable = getResources().getDrawable(R.drawable.img_section1);
title = getResources().getString(R.string.title_section1);
break;
case 1:
myDrawable = getResources().getDrawable(R.drawable.img_section2);
title = getResources().getString(R.string.title_section2);
break;
case 2:
myDrawable = getResources().getDrawable(R.drawable.img_section3);
title = getResources().getString(R.string.title_section3);
break;
case 3:
myDrawable = getResources().getDrawable(R.drawable.img_section4);
title = getResources().getString(R.string.title_section4);
break;
case 4:
myDrawable = getResources().getDrawable(R.drawable.img_section5);
title = getResources().getString(R.string.title_section5);
break;
default:
//TODO: handle default selection
break;
}
SpannableStringBuilder sb = new SpannableStringBuilder(" " + title); // space added before text for convenience
try {
myDrawable.setBounds(5, 5, myDrawable.getIntrinsicWidth(), myDrawable.getIntrinsicHeight());
ImageSpan span = new ImageSpan(myDrawable, DynamicDrawableSpan.ALIGN_BASELINE);
sb.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return sb;
} catch (Exception e) {
// TODO: handle exception
}
谢谢你.
这篇关于如何在Android FragmentPagerAdapter中添加页面标题和图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文