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

前端 未结 8 902
傲寒
傲寒 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());
    }
    
    0 讨论(0)
  • 2020-12-15 04:50
     <android.support.design.widget.TabLayout
         android:id="@+id/tab_layout"
         android:layout_width="210dp"
         android:layout_height="28dp"
         android:layout_centerInParent="true"
         android:background="@drawable/bg_forum_tab"
         app:tabIndicatorColor="@color/colorBtnBg"
         app:tabIndicatorHeight="0dp"
         app:tabPaddingBottom="-1dp"
         app:tabPaddingEnd="-1dp"
         app:tabPaddingStart="-1dp"
         app:tabPaddingTop="-1dp"
         app:tabSelectedTextColor="@color/colorBtnBg"
         app:tabTextColor="@color/colorWhite" />
    

    Set tabPaddingStart/tabPaddingEnd/tabPaddingTop/tabPaddingBottom like this.

    0 讨论(0)
提交回复
热议问题