TabLayout setIcon具有一定的大小

时间:2017-04-30 10:02:09

标签: android icons android-tablayout

我希望我的标签图标看起来更大,我不知道该怎么做,因为这是我第一次在android中使用标签。

我目前正在使用这样的图标:

 for (int i = 0; i < tabLayout.getTabCount(); i++) {
        if (i==0)
           tabLayout.getTabAt(i).setIcon(R.drawable.perfil);
        if (i==1)
           tabLayout.getTabAt(i).setIcon(R.drawable.amigos);
        if (i==2)
           tabLayout.getTabAt(i).setIcon(R.drawable.nueva);
        if (i==3)
           tabLayout.getTabAt(i).setIcon(R.drawable.notificacion0);
        if (i==4)
           tabLayout.getTabAt(i).setIcon(R.drawable.buscar);
    }

我想也许你可以创建一个位图或类似的,给它我想要的宽度和高度,但我不知道如何实现它

我写了完整的代码,希望有人知道如何提供帮助。非常感谢你:

public class MainClass extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager viewPager;

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

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

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

        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            if (i==0)
                tabLayout.getTabAt(i).setIcon(R.drawable.perfil);
            if (i==1)
                tabLayout.getTabAt(i).setIcon(R.drawable.amigos);
            if (i==2)
                tabLayout.getTabAt(i).setIcon(R.drawable.nueva);
            if (i==3)
                tabLayout.getTabAt(i).setIcon(R.drawable.notificacion0);
            if (i==4)
                    tabLayout.getTabAt(i).setIcon(R.drawable.buscar);
        }

    }

    private void addTabs(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new Tablayout_in_Android());
        adapter.addFrag(new Tablayout_in_Android());
        adapter.addFrag(new Tablayout_in_Android());
        adapter.addFrag(new Tablayout_in_Android());
        adapter.addFrag(new Tablayout_in_Android());
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = 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) {
            mFragmentList.add(fragment);
        }

    }


}

和班级 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);
    }

}

1 个答案:

答案 0 :(得分:0)

在此论坛中搜索,我发现了一个可以解决问题的问题:https://stackoverflow.com/questions/38424126/change-icon-size-of-tabs

我所做的就是创建xml并在将每个图标分配给每个标签后添加答案中的代码。

谢谢,我希望有人为你服务。

我留下代码:

for (int i = 0; i < tabLayout.getTabCount(); i++) {
            if (i==0)
                tabLayout.getTabAt(i).setIcon(R.drawable.perfil);
            if (i==1)
                tabLayout.getTabAt(i).setIcon(R.drawable.amigos);
            if (i==2)
                tabLayout.getTabAt(i).setIcon(R.drawable.nueva);
            if (i==3)
                tabLayout.getTabAt(i).setIcon(R.drawable.notificacion0);
            if (i==4)
                    tabLayout.getTabAt(i).setIcon(R.drawable.buscar);
        }

        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null) tab.setCustomView(R.layout.view_home_tab);
        }