Sto usando drawable vettore come le mie immagini scheda, questo funziona solo su API> 21. Ma sono sicuro che puoi semplicemente usare i drawable dell'immagine allo stesso modo.
Ecco il mio codice:
class MyPagerAdapter extends FragmentPagerAdapter {
private String[] tabText = getResources().getStringArray(R.array.tabs);
public MyPagerAdapter(FragmentManager fm) {
super(fm);
tabText = getResources().getStringArray(R.array.tabs);
}
@Override
public Fragment getItem(int position) {
Fragment fragment=null;
if (position == 0)
fragment = new FragmentA();
if (position == 1)
fragment = new FragmentB();
if (position == 2)
fragment=new FragmentC();
return fragment;
}
@Override
public CharSequence getPageTitle(int position) {
SpannableString spannableString = null;
if (position == 0) {
//use the MrVector library to inflate vector drawable inside tab
Drawable drawable = MrVector.inflate(getResources(), R.drawable.vector_add);
//set the size of drawable to 36 pixels
drawable.setBounds(0, 0, 36, 36);
ImageSpan imageSpan = new ImageSpan(drawable);
//to make our tabs icon only, set the Text as blank string with white space
spannableString = new SpannableString(" ");
spannableString.setSpan(imageSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (position == 1) {
//use the MrVector library to inflate vector drawable inside tab
Drawable drawable = MrVector.inflate(getResources(), R.drawable.vector_list);
//set the size of drawable to 36 pixels
drawable.setBounds(0, 0, 36, 36);
ImageSpan imageSpan = new ImageSpan(drawable);
//to make our tabs icon only, set the Text as blank string with white space
spannableString = new SpannableString(" ");
spannableString.setSpan(imageSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (position == 2) {
//use the MrVector library to inflate vector drawable inside tab
Drawable drawable = MrVector.inflate(getResources(), R.drawable.vector_settings);
//set the size of drawable to 36 pixels
drawable.setBounds(0, 0, 36, 36);
ImageSpan imageSpan = new ImageSpan(drawable);
//to make our tabs icon only, set the Text as blank string with white space
spannableString = new SpannableString(" ");
spannableString.setSpan(imageSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return spannableString;
}
@Override
public int getCount() {
return 3;
}
Con le immagini:
@Override
public CharSequence getPageTitle(int position) {
SpannableStringBuilder sb = new SpannableStringBuilder(" ");
if (position == 0) {
Drawable drawable = getDrawable(R.drawable.ic_action_add);
drawable.setBounds(0, 0, 48, 48);
ImageSpan imageSpan = new ImageSpan(drawable);
//to make our tabs icon only, set the Text as blank string with white space
sb.setSpan(imageSpan, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (position == 1) {
Drawable drawable = getDrawable(R.drawable.ic_action_list_2);
drawable.setBounds(0, 0, 48, 48);
ImageSpan imageSpan = new ImageSpan(drawable);
//to make our tabs icon only, set the Text as blank string with white space
sb.setSpan(imageSpan, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return sb;
}
MrVector liabrary: https://github.com/telly/MrVector
Uno strumento che uso per convertire SVG a VectorDrawable: http://inloop.github.io/svg2android/