How can I create tabs of the same size in TabLayout

I use DesignSupportLibrary (v22.2.0) and want the TabLayout tabs to be the same width - regardless of the length of the tab text. I tried MODE_FIXED, but it still shows tabs with different widths. Here is the xml:

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:tabMode="fixed"/>
+4
source share
1 answer

If you want to specify a minimum width for each tab, you can set it in the style of:

<style name="MyTabLayoutStyle" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="tabMinWidth">100dp</item>
</style>

and then set the theme to this style (you can also remove the tabMode attribute):

<android.support.design.widget.TabLayout
  android:id="@+id/tab_layout"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="?attr/colorPrimary"
  android:theme="@style/MyTabLayoutStyle"/>

Alternatively, you can set tabMinWidth directly to TabLayout:

<android.support.design.widget.TabLayout
  android:id="@+id/tab_layout"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="?attr/colorPrimary"
  android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
  app:tabMinWidth="100dp"/>

: , MODE_FIXED , layout_width TabLayout ( "wrap_content" ). - ( ). , , . .

+7

Source: https://habr.com/ru/post/1598942/


All Articles