Android设计支持库:TabLayout标签文本为单行

时间:2016-01-01 10:31:00

标签: android android-layout android-viewpager android-view android-tablayout

我想将标签文本(最多8个字符)放在单行

enter image description here

我为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"
  ..
  ..
  />

3 个答案:

答案 0 :(得分:14)

只需将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" /> 

答案 1 :(得分:1)

更改以下代码后

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

答案 2 :(得分:0)

将此行添加到TabLayout自定义样式中,将起到神奇的作用。

export class AppComponent { name = 'from Angular' constructor(@Inject(Service) service, @Inject(ViewContainerRef) viewContainerRef) { service.setRootViewContainerRef(viewContainerRef) service.addDynamicComponent() }