如何在我的应用程序android中添加可滑动标签的图标

时间:2017-02-15 01:11:03

标签: android tabs icons

如何在我的应用程序android中添加可滑动标签的图标:

  

大家好,我想用图标显示标签?只需改变短信和   在那里制作图标!什么是我需要改变的其他文件?

这是我的代码:

public class MainActivity extends ActionBarActivity implements MaterialTabListener {

    MaterialTabHost tabHost;
    ViewPager pager;
    ViewPagerAdapter adapter;

    private String[] tabs = {
        "Tab2",
        "Tab3",
        "Tab1"
    };

    Toolbar toolbar;
    private Menu menu;
    private AdView mAdView;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        OneSignal.startInit(this).init();
        setContentView(R.layout.activity_main);
        toolbar = (android.support.v7.widget.Toolbar) this.findViewById(R.id.toolbar);
        toolbar.setTitle(getString(R.string.app_name));
        this.setSupportActionBar(toolbar);

        // Look up the AdView as a resource and load a request.
        mAdView = (AdView) findViewById(R.id.adView);
        mAdView.loadAd(new AdRequest.Builder().build());

        tabHost = (MaterialTabHost) this.findViewById(R.id.tabHost);
        pager = (ViewPager) this.findViewById(R.id.pager);
        adapter = new ViewPagerAdapter(getSupportFragmentManager());
        pager.setAdapter(adapter);

        pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {@Override
            public void onPageSelected(int position) {
                // when user do a swipe the selected tab change
                tabHost.setSelectedNavigationItem(position);

            }
        });

        for (String tab_name: tabs) {
            tabHost.addTab(
            tabHost.newTab().setText(tab_name).setTabListener(MainActivity.this));
        }

    }

    @Override
    public void onTabSelected(MaterialTab tab) {
        // TODO Auto-generated method stub
        pager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabReselected(MaterialTab tab) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabUnselected(MaterialTab tab) {
        // TODO Auto-generated method stub

    }

    private class ViewPagerAdapter extends FragmentStatePagerAdapter {

    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);

    }

    public Fragment getItem(int num) {
        switch (num) {
        case 0:
            return new LatestFragment();
        case 1:
            return new AllPhotosFragment();
        case 2:
            return new FavoriteFragment();
        }
        return null;
    }

    @Override
    public int getCount() {
        return 3;
    }

}

0 个答案:

没有答案