【问题标题】:How to change font size in Material Design TabLayout android?如何在 Material Design TabLayout android中更改字体大小?
【发布时间】:2022-02-11 14:59:41
【问题描述】:

我一直在尝试在线探索以设置 TabLayout TabItems 的字体大小 但它们似乎都不适合我。这是我上次尝试引用的link

以下是我目前已经实现的东西, 不知道为什么它没有从样式中选择textSize

在 xml 中:

<com.google.android.material.tabs.TabLayout
                android:id="@+id/tabLayoutForgot"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/header_height"
                app:layout_constraintTop_toBottomOf="@+id/imageViewLogo"
                app:tabIndicatorColor="@color/text_color"
                app:tabSelectedTextColor="@color/text_color"
                app:tabGravity="fill"
                app:tabTextAppearance="@style/MyCustomTabLayout"
                app:tabMode="fixed"
                app:tabIndicatorFullWidth="false"
                app:tabTextColor="@color/text_color" />

在themes.xml中:

<style name="MyCustomTabLayout" parent="@android:style/TextAppearance.Widget.TabWidget">
        <item name="textAllCaps">false</item>
        <item name="android:textSize">20sp</item>
        <item name="android:fontFamily">@font/roboto_medium</item>
        <item name="android:textColor">@color/text_color</item>
    </style>

这会像textAllCapsfontFamily 一样执行其他所有操作,但似乎不会占用textSize

感谢任何帮助。

【问题讨论】:

    标签: android material-design android-tablayout


    【解决方案1】:

    你应该从TextAppearance.Design.Tab继承你的风格 先做个样式,

    <style name="MyStyle" parent="TextAppearance.Design.Tab">
        <item name="android:textSize">16sp</item>
    </style>
    

    然后像这样在你的 Tablayout 中使用它,

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

    希望它会起作用

    【讨论】:

    • 嗨,我会尝试一下,顺便说一句,我使用的是 com.google.android.material.tabs.TabLayout 而不是 android.support.design.widget.TabLayout 不确定这是否会影响任何事情。但会在这里尝试更新
    • 我猜它会正常工作的。如果不告诉我。
    • 它仍然无法正常工作:(
    【解决方案2】:

    嗯,我找到了解决问题的方法。

    dimens.xml 中添加以下代码行使其对我有用。

    &lt;dimen name="design_tab_text_size_2line" tools:override="true"&gt;20sp&lt;/dimen&gt;

    【讨论】:

      【解决方案3】:

      试试stylecom.google.android.material.tabs.TabLayout

      将主样式的parent添加为@style/Widget.MaterialComponents.TabLayout

      tabTextAppearanceparent样式添加为@style/Widget.AppCompat.TextView

      <style name="MyCustomTabLayout" parent="@style/Widget.MaterialComponents.TabLayout">
          <item name="tabSelectedTextColor">@color/black</item>
          <item name="tabTextColor">@color/purple_700</item>
          <item name="tabIndicatorColor">@color/black</item>
          <item name="tabTextAppearance">@style/TabText</item>
      </style>
      
      <style name="TabText" parent="@style/Widget.AppCompat.TextView">
          <item name="textAllCaps">false</item>
          <item name="android:textSize">20sp</item>
      </style>
      

      在 XML 中:

      <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabLayoutForgot"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            app:tabGravity="fill"
            style="@style/MyCustomTabLayout"
            app:tabMode="fixed"
            app:tabIndicatorFullWidth="false"/>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-17
        • 1970-01-01
        • 2021-01-20
        • 1970-01-01
        • 2019-03-29
        • 2021-02-22
        • 1970-01-01
        • 2020-10-11
        相关资源
        最近更新 更多