我正在使用designsupportlibrary(v22.2.0)并希望tablayout中的选项卡具有相同的宽度,而不考虑选项卡文本的长度。我已经尝试了模式修正,但它仍然显示不同宽度的标签。以下是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"/>
最佳答案
如果要为每个选项卡指定最小宽度,可以在样式中进行设置:
<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的布局宽度(而不是使用“wrap_content”),否则mode_fixed似乎无法正常工作。但是,选项卡仍将仅展开以匹配最宽的选项卡(由最长文本长度决定)。因此,如果定义的宽度大于生成的选项卡。选项卡右侧将有一个空格。