Cannot remove Padding from Tabs when using Custom views with Tab Layout

前端 未结 8 915
傲寒
傲寒 2020-12-15 04:18

I have added a Relative Layout in Custom View and have added this in Tab Layout. I am using a white background for tabs and have not applied any padding in tabs custom layou

8条回答
  •  庸人自扰
    2020-12-15 04:43

    Through XML you can remove only the left and right paddings like that:

    app:tabPaddingEnd="0dp"
    app:tabPaddingStart="0dp"
    

    Notice, that this way doesn`t work for TOP and BOTTOM paddings, but I find next solution:

    When you use custom views as tab item, you need to set LayoutParams to the view and set paddings 0.

    for (int i = 0; i < tabLayout.getTabCount(); i++) {           
        View tabView = LayoutInflater.from(context)
                .inflate(LayoutInflater.from(this), R.layout.item_main_tabview, null, false);
    
        tabView.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        tabView.setPadding(0, 0, 0, 0);
        tabLayout.getTabAt(i).setCustomView(tabViewBinding.getRoot());
    }
    

提交回复
热议问题