0
votes

My TabLayout requires this

  1. Equidistant text like this

  2. Left and right padding are constant.

  3. All texts should be the same size

  4. Tabs should not be scrollable *** the texts on each tab can have any length of characters

1

1 Answers

1
votes

Create a new style in styles.xml

<!-- TabTextSizeAppearance to define your tab text size -->
<style name="TabTextSizeAppearance" parent="TextAppearance.AppCompat.Caption">
    <item name="android:textSize">14sp</item>
    <item name="android:textStyle">normal</item>
</style>

<!-- MyTabLayout to define TabLayout parameters -->
<style name="MyTabLayout" parent="Widget.Design.TabLayout">
    <item name="tabGravity">fill</item>
    <item name="tabMode">fixed</item>
    <item name="tabPaddingStart">4dp</item>
    <item name="tabPaddingEnd">4dp</item>
    <item name="tabSelectedTextColor">@color/black</item>
    <item name="tabTextAppearance">@style/TabTextSizeAppearance</item>
</style>

And finally, add this to your dimens.xml

<dimen name="design_tab_text_size_2line" tools:ignore="PrivateResource">14sp</dimen>

This dimen size should be the same as TabTextSizeAppearance text size to ensure text is the same size even if with long text

Then use it in your xml

<android.support.design.widget.TabLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/MyTabLayout"
   >

    <android.support.design.widget.TabItem
      ...


</android.support.design.widget.TabLayout>