-
StackOverflow 文档
-
Android 教程
-
使用图标字体增强 Android 性能
-
带有图标字体的 TabLayout
public class TabAdapter extends FragmentPagerAdapter {
CustomTypefaceSpan fonte;
List<Fragment> fragments = new ArrayList<>(4);
private String[] icons = {"\ue001","\uE002","\uE003","\uE004"};
public TabAdapter(FragmentManager fm, CustomTypefaceSpan fonte) {
super(fm);
this.fonte = fonte
for (int i = 0; i < 4; i++){
fragments.add(MyFragment.newInstance());
}
}
public List<Fragment> getFragments() {
return fragments;
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public CharSequence getPageTitle(int position) {
SpannableStringBuilder ss = new SpannableStringBuilder(icons[position]);
ss.setSpan(fonte,0,ss.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
ss.setSpan(new RelativeSizeSpan(1.5f),0,ss.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE );
return ss;
}
@Override
public int getCount() {
return 4;
}
}
- 在此示例中,myfont.ttf 位于 Assets 文件夹中。创建资产文件夹
- 在你的活动课上
//..
TabLayout tabs;
ViewPager tabs_pager;
public CustomTypefaceSpan fonte;
//..
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//...
fm = getSupportFragmentManager();
fonte = new CustomTypefaceSpan("icomoon",Typeface.createFromAsset(getAssets(),"myfont.ttf"));
this.tabs = ((TabLayout) hasViews.findViewById(R.id.tabs));
this.tabs_pager = ((ViewPager) hasViews.findViewById(R.id.tabs_pager));
//...
}
@Override
protected void onStart() {
super.onStart();
//..
tabs_pager.setAdapter(new TabAdapter(fm,fonte));
tabs.setupWithViewPager(tabs_pager);
//..