如何在TabLayout中创建相同大小的标签页

5

我正在使用DesignSupportLibrary(v22.2.0),希望TabLayout中的选项卡具有相同的宽度 - 不管选项卡文本的长度如何。我已经尝试了MODE_FIXED,但仍然显示不同宽度的标签页。这是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"/>
1个回答

8
如果您想为每个选项卡指定最小宽度,可以在样式中设置:
<style name="MyTabLayoutStyle" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="tabMinWidth">100dp</item>
</style>

然后将主题设置为此样式(您也可以删除tabMode属性):
<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"/>

或者,您可以在TabLayout中直接设置tabMinWidth:

<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"/>

另外需要说明的是:只有在定义TabLayout的layout_width属性(而非使用“wrap_content”)后,MODE_FIXED才能正常工作。但是选项卡仍然只会扩展到与最宽的选项卡匹配(由文本的最大长度确定)。因此,如果您定义的宽度大于生成的选项卡,则选项卡右侧将出现空白区域。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接