为什么我无法向标签添加图标?

时间:2017-06-04 16:47:52

标签: java android android-viewpager android-tablayout

我使用可绘制的向量创建了一个数组,并将它们添加到选项卡,但我的应用程序在开始时崩溃了。我认为问题出在我调用setupTabIcons方法的地方,但我无法弄清楚要做什么或在哪里调用它。这是我设置适配器和tablayout的类。

public class TabFragment extends Fragment  {
    public static TabLayout tabLayout;
    public static ViewPager viewPager;
    public static int int_items = 3 ;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        // Inflate TabLayout

        View x =  inflater.inflate(R.layout.tab_layout,null);
        tabLayout = (TabLayout) x.findViewById(R.id.tabs);
        viewPager = (ViewPager) x.findViewById(R.id.viewpager);


        // Setup Adapter for ViewPager

        viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));

        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                tabLayout.setupWithViewPager(viewPager);
            }
        });
        setupTabIcons();

        return x;

    }

    class MyAdapter extends FragmentPagerAdapter{

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }


        @Override
        public Fragment getItem(int position)
        {
            switch (position){
                case 0 : return new Fragment1();
                case 1 : return new Fragment2();
                case 2 : return new Fragment3();
            }
            return null;
        }

        @Override
        public int getCount() {

            return int_items;

        }

            @Override
        public CharSequence getPageTitle(int position) {

            switch (position){
                case 0 :
                    return "1";
                case 1 :
                    return "2";
                case 2 :
                    return "3";
            }
            return null;
        }
    }
    private int[] tabIcons = {
            R.drawable.icon_tabs_1,
            R.drawable.icon_tabs_2,
            R.drawable.icon_tabs_3
    };

    public void setupTabIcons() {
        tabLayout.getTabAt(0).setIcon(tabIcons[0]);
        tabLayout.getTabAt(1).setIcon(tabIcons[1]);
        tabLayout.getTabAt(2).setIcon(tabIcons[2]);
    }



}

这是主要的活动......

public class MainActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mToggle;

    NavigationView mNavigationView;
    FragmentManager mFragmentManager;
    FragmentTransaction mFragmentTransaction;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Navigation drawer setup

        mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open,R.string.close);

        mDrawerLayout.addDrawerListener(mToggle);
        mToggle.syncState();

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        mFragmentManager = getSupportFragmentManager();
        mFragmentTransaction = mFragmentManager.beginTransaction();
        mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if(mToggle.onOptionsItemSelected(item)){
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:0)

尝试此方法,因为setupTabIcons()方法确实存在问题: https://guides.codepath.com/android/google-play-style-tabs-using-tablayout#add-icons-to-tablayout