How to increase icon size of tabs in TabLayout

后端 未结 6 886
刺人心
刺人心 2020-12-05 00:43

I am trying to increase icon size of tabs in my app. Icon sizes are fixed tried out many ways but nothing is working, finally tried the following but no change in size.Pleas

6条回答
  •  既然无缘
    2020-12-05 01:11

    Hi I faced the same problem before and this is the best solution I could find:

    You should use (setCustomView), first of all make a new layout file lets name it (customtab.xml):

    
    
    
        
    
    

    Then and for each tab do this: (use the same layout .xml)

    View view1 = getLayoutInflater().inflate(R.layout.customtab, null);
    view1.findViewById(R.id.icon).setBackgroundResource(R.drawable.my1);
    tabLayout.addTab(tabLayout.newTab().setCustomView(view1));
    
    
    View view2 = getLayoutInflater().inflate(R.layout.customtab, null);
    view2.findViewById(R.id.icon).setBackgroundResource(R.drawable.my2);
    tabLayout.addTab(tabLayout.newTab().setCustomView(view2));
    
    
    View view3 = getLayoutInflater().inflate(R.layout.customtab, null);
    view3.findViewById(R.id.icon).setBackgroundResource(R.drawable.my3);
    tabLayout.addTab(tabLayout.newTab().setCustomView(view3));
    
    ...
    

    OR in a similar way

    public static final int[] tabIcon = {R.drawable.icon_one, R.drawable.icon_two, R.drawable.icon_three};
    
    private void setCustomTabs() {
    
        for (int i = 0; i < tabIcon.length; i++) {
            View view = getLayoutInflater().inflate(R.layout.customtab,null);
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            view.findViewById(R.id.icon).setBackgroundResource(tabIcon[i]);
            if(tab!=null) tab.setCustomView(view);
         }
     }
    

提交回复
热议问题