如何用图标做标签

How to do tabs with icons

我正在尝试 activity 使用选项卡和分页视图 我在博客中找到了这段代码,但它不像让选项卡有图标而不是文本那样......我把代码:

ViewPagerAdapter.java

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFrag(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

Tablayout_in_Android.java

public class Tablayout_in_Android extends Fragment {

    public Tablayout_in_Android() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_tablayout_in__android, container, false);
    }

}

方法 onCreate main class

 viewPager = (ViewPager) findViewById(R.id.viewpager);
        addTabs(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.pestañas);
        tabLayout.setupWithViewPager(viewPager);

    }

    private void addTabs(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new Tablayout_in_Android(), "ONE");
        adapter.addFrag(new Tablayout_in_Android(), "TWO");
        adapter.addFrag(new Tablayout_in_Android(), "THREE");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }

如何更改图标的文字?谢谢

试试这个:

tabLayout.setupWithViewPager(mViewPager);
for (int i = 0; i < mTabLayout.getTabCount(); i++) {
    tabLayout.getTabAt(i).setIcon(R.drawable.your_icon);
}

要扩展 Jonathan 的回答,您还可以在选项卡中显示一个图标列表。 确保图标与您拥有的选项卡数量相匹配。

tabLayout.setupWithViewPager(mViewPager);

int[] tabIcons = {
        R.drawable.ic_icon_1,
        R.drawable.ic_icon_2,
        R.drawable.ic_icon_3
};

for(int i=0; i<tabLayout.getTabCount(); i++){
    if(tabLayout.getTabAt(i) != null){
        tabLayout.getTabAt(i).setIcon(tabIcons[i]);
    }
}

此外,您可能希望图标和选项卡标签处于水平方向,在 TabLayout 的 xml 布局中使用 app:tabInlineLabel="true"