How to add margin between tabs in TabLayout?

前端 未结 5 1582
渐次进展
渐次进展 2020-12-06 05:18

Is there a way to add margin between the tabs in a TabLayout? I\'ve tried with using a custom style for Widget.Design.TabLayout, but there are properties on

5条回答
  •  渐次进展
    2020-12-06 06:04

    @Todor Kostov answered well, but the center of the tabs are slipped away because the last tab has margin too.

    so use mTabLayout.getTabCount() - 1 instead of just mTabLayout.getCodeCount().

            for(int i=0; i < mTabLayout.getTabCount() - 1; i++) {
                View tab = ((ViewGroup) mTabLayout.getChildAt(0)).getChildAt(i);
                ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) tab.getLayoutParams();
                p.setMargins(0, 0, 50, 0);
                tab.requestLayout();
            }
    

提交回复
热议问题