TabLayout sets the intervals or fields for each tab.

I want to set the border between each tab. Like PagerTabStrip, which has setTextSpacing (int textSpacing) to span tabs. Can TabLayout do this?

+5
source share
5 answers

I struggled with this problem for a while, found a solution on this topic: Android TabLayout design support library using custom tab layouts, but tab wrapping layout

<!-- Add the padding to tabPaddingStart and/or tabPaddingEnd --> <android.support.design.widget.TabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="@dimen/tab_layout_height" app:tabPaddingStart="10dp" app:tabPaddingEnd="10dp"> 
+21
source

You can use the tabMinWidth attribute. like this.

 <android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="44dp" app:tabIndicatorColor="@color/default_enable" app:tabTextColor="@color/font_default_03" app:tabSelectedTextColor="@color/default_enable" app:tabMinWidth="50dp" android:clipToPadding="false" android:paddingLeft="4dp" android:paddingRight="4dp" /> 
+3
source

layout

 <android.support.design.widget.TabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="wrap_content"/> 

Java

 TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout); int betweenSpace = 100; ViewGroup slidingTabStrip = (ViewGroup) tabLayout.getChildAt(0); for (int i=0; i<slidingTabStrip.getChildCount()-1; i++) { View v = slidingTabStrip.getChildAt(i); ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) v.getLayoutParams(); params.rightMargin = betweenSpace; } 
0
source

This is how the field is set for four different tabs. You can change the values ​​of the setMargins functions (v1, v2, v3, v4) to get a suitable setting for the number of tabs you work with. Hope this helps. Note that tabHost is a TabHost object that hosts the different tabs that you work with.

 Display display = getWindowManager().getDefaultDisplay(); int width = display.getWidth(); View currentView; for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) { //This code removes divider between tabs tabHost.getTabWidget().setDividerDrawable(null); tabHost.getTabWidget().getChildAt(i).setLayoutParams(new LinearLayout.LayoutParams((width/8)-2,50)); currentView = tabHost.getTabWidget().getChildAt(i); LinearLayout.LayoutParams currentLayout = (LinearLayout.LayoutParams) currentView.getLayoutParams(); currentLayout.setMargins(30, 5, 80, 0); } 
-1
source
 <android.support.design.widget.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabMinWidth="0dp" app:tabMode="scrollable" /> 
-1
source

All Articles