Android 设计支持库:单行 TabLayout 选项卡文本

Android Design Support Library: TabLayout tabs text in single line

我想在单行中放置制表符文本(最多 8 个字符)

我为 TabLayout 元素添加了自定义样式

<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
    <item name="tabTextAppearance">@style/MyCustomTabTextAppearance</item>
</style>
<style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">
    <item name="android:maxLines">1</item>
    <item name="android:singleLine">true</item>
</style>

还是不行。我也试过了

 <android.support.design.widget.TabLayout
  app:tabTextAppearance="@style/MyCustomTabTextAppearance"
  ..
  ..
  />

一旦更改以下代码

<style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">
    <item name="android:maxLines">1</item>
    <item name="android:singleLine">true</item>
</style>

以下

<style name="MyCustomTabTextAppearance" parent="TextAppearance.AppCompat.Button">
    <item name="android:maxLines">1</item>
    <item name="android:singleLine">true</item>
</style>

只需将 app:tabMode="scrollable" 添加到 TabLayout;

<android.support.design.widget.TabLayout 
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/sliding_tabs" 
android:layout_width="match_parent" 
app:tabMode="scrollable"
android:layout_height="wrap_content" /> 

将这一行添加到您的 TabLayout 自定义样式中,它会产生神奇效果。

<item name="android:maxLines">1</item>