如何更改选择Android标签栏数组上的图标?

时间:2019-01-06 06:01:32

标签: android android-studio android-tabs

因此,我使用要显示的图标阵列构建此选项卡栏,但是无法弄清楚当选择选项卡时如何实现新图标。 我制作了一个与图像相对应的字符串A,B,C,D数组。 我还要测试位置并相应地更改片段。 这是我的代码:

public class MyAdapter extends FragmentPagerAdapter {
private Context mContext;
private String[] titles ={"A","B","C","D"};
int[] icon = new int[]{R.drawable.home_tab, R.drawable.peak_tab,R.drawable.user_tab,R.drawable.news_tab};
private int heightIcon;

public MyAdapter(FragmentManager fm, Context c){
    super(fm);
    mContext = c;
    double scale = c.getResources().getDisplayMetrics().density;
    heightIcon=(int)(30*scale+0.5f);
}
@Override
public Fragment getItem(int position) {
    Fragment frag= null;

    if(position ==0){
        frag = new FeedsFragment();
    }else if(position == 1){
        frag = new LitFragment();
    }else if(position == 2){
        frag = new ProfileFragment();
    }else if(position == 3){
        frag = new CommunityFragment();
    }

    Bundle b = new Bundle();
    b.putInt("position", position);
    frag.setArguments(b);
    return frag;

}

@Override
public int getCount() {
    return titles.length;
}

public CharSequence getPageTitle(int position){
    Drawable d = mContext.getResources().getDrawable(icon[position]);
    d.setBounds(0,0,heightIcon,heightIcon);
    ImageSpan is = new ImageSpan(d);

    SpannableString sp = new SpannableString(" ");
    sp.setSpan(is,0,sp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    return sp;
}

}

0 个答案:

没有答案