-
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);
//..