此外,如果它是当前选定的选项卡,我想更改图标颜色. 解决方法 使用它来填充vIEwPager:
public class SectionPagerAdapter extends FragmentPagerAdapter { public SectionPagerAdapter(FragmentManager fm) { super(fm); } @OverrIDe public Fragment getItem(int position) { switch (position) { case 0: return mFragmentA; case 1: return mFragmentB; case 2: return mFragmentC; default: return null; } } @OverrIDe public int getCount() { return 3; } @OverrIDe public CharSequence getPageTitle(int position) { return ""; // This removes the Title,as you wish }}
然后在onCreateVIEw中:
final TabLayout tabLayout = (TabLayout) v.findVIEwByID(R.ID.tab_layout);final VIEwPager vIEwPager = (VIEwPager) v.findVIEwByID(R.ID.vIEw_pager);vIEwPager.setAdapter(new SectionPagerAdapter(getActivity().getSupportFragmentManager()));tabLayout.setupWithVIEwPager(vIEwPager);
也方便了解:
for (int i = 0; i < tabLayout.getTabCount(); i++) { int iconID = -1; switch (i) { case 0: iconID = R.drawable.icon1; break; case 1: iconID = R.drawable.icon2; break; case 2: iconID = R.drawable.icon3; break; } tabLayout.getTabAt(i).setIcon(iconID);}// Needed since support librarIEs version 23.0.0 vIEwPager.addOnPagechangelistener(new VIEwPager.OnPagechangelistener() { @OverrIDe public voID onPageScrolled(int position,float positionOffset,int positionOffsetPixels) { } @OverrIDe public voID onPageSelected(int position) { tabLayout.getTabAt(position).select(); } @OverrIDe public voID onPageScrollStateChanged(int state) { }});tabLayout.setonTabSelectedListener(new TabLayout.VIEwPagerOnTabSelectedListener(vIEwPager) { @OverrIDe public voID onTabSelected(TabLayout.Tab tab) { super.onTabSelected(tab); }});
并更改选定的标签颜色:
tabLayout.setSelectedTabIndicatorcolor(colorID); // For the line if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LolliPOP) { for (int i = 0; i < tabLayout.getTabCount(); i++) { tabLayout.getTabAt(i).getIcon().setTint(getResources().getcolor(R.color.gray)); } tabLayout.getTabAt(selectedTabposition).getIcon().setTint(colorID); }总结
以上是内存溢出为你收集整理的android – 仅支持图标的设计支持库选项卡全部内容,希望文章能够帮你解决android – 仅支持图标的设计支持库选项卡所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)