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

前端 未结 8 906
傲寒
傲寒 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:41

    I solved this by setting the margin & padding of your custom view's parent to zero, when adding new tabs to tab layout.

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    lp.setMargins(0,0,0,0);
        
    TabLayout.Tab newTab = mTabsBottom.newTab().setCustomView(R.layout.view_custom_tab);
    setupTabParentLayout(newTab.getCustomView(), lp);
    ..
    ..
    ..
    private void setupTabParentLayout(View customView, LinearLayout.LayoutParams lp) {
        LinearLayout tabParent = (LinearLayout) customView.getParent();
        tabParent.setLayoutParams(lp);
        tabParent.setPadding(0,0,0,0);
    }
    

    Trick here was to use LinearLayout.LayoutParams for custom view's parent.

提交回复
热议问题