问题
HI i am trying to add image to the new TabLayout introduced in android support design. But it doesn't seems to be working i took some code github and tried to do some little change except for text nothing is workin no icon is showing on tha tab instead of text.
Any help is appreciated.
List<String> titles = new ArrayList<>();
titles.add("Page One");
titles.add("Page Two");
mTabLayout.addTab(mTabLayout.newTab().setIcon(getResources().getDrawable(R.drawable.ic_launcher)));
mTabLayout.addTab(mTabLayout.newTab().setIcon(getResources().getDrawable(R.drawable.ic_launcher)));
List<Fragment> fragments = new ArrayList<>();
fragments.add(new AnimatedFragment());
fragments.add(new AnimatedFragment());
FragmentAdapter adapter =
new FragmentAdapter(getSupportFragmentManager(), fragments, titles);
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(0);
mTabLayout.setupWithViewPager(viewPager);
mTabLayout.setTabsFromPagerAdapter(adapter);
This is the code where i setup tablayout and viewpager.
回答1:
You can add icons to tablayout from viewPager's getPageTitle method. Here is a good tutorial tutorial to do so that I tried.
来源:https://stackoverflow.com/questions/30950999/customize-new-tablayout-in-support-design-library