how to add the icon for swipeable tabs

女生的网名这么多〃 提交于 2019-11-27 06:54:47

If you are using a TabLayout, just do this (This example uses three tabs):

 //An array containing your icons from the drawable directory
 final int[] ICONS = new int[]{
            R.drawable.icon_1,
            R.drawable.icon_2,
            R.drawable.icon_3
    };

    //Get reference to your Tablayout
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

    tabLayout.getTabAt(0).setIcon(ICONS[0]);
    tabLayout.getTabAt(1).setIcon(ICONS[1]);
    tabLayout.getTabAt(2).setIcon(ICONS[2]);
Jake JediMastr Ellis

Add .setIcon(resources.getDrawable(DrawableIDHere)) in your for loop, and also alter your for loop a bit.

// Adding Tabs
for (int i=0; i < tabs.length; i++)
{
actionBar.addTab(actionBar.newTab().setText(tabs[i])
                         .setIcon(resources.getDrawable(ICONS[i]))
                         .setTabListener(this));
}//endfor

Also, don't forget to put the right drawable ID's in your ICONS array!

// here you add an array of your icon   
 final int[] ICONS = new int[] {
                    R.drawable.ic_launcher,
                    R.drawable.ic_launcher,
                    R.drawable.ic_launcher,
                    R.drawable.ic_launcher,
            };

    // add this following code to solve your problem
    // here NewsFeedActivity.this.getResources() is used to get the drawable folder resource
    // instead of NewsFeedActivity you have to use your activity name here  
    for (int i=0; i < tabs.length; i++)
            {
            actionBar.addTab(actionBar.newTab().setText(tabs[i])
                                     .setIcon(NewsFeedActivity.this.getResources().getDrawable(ICONS[i]))
                                     .setTabListener(this));
            }//endfor

Hi check out the ViewPagerIndicator library.

This is very good example for swipe-able tabs.

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