Customize new TabLayout in support design library

流过昼夜 提交于 2019-12-13 02:04:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!