【问题标题】:How can one apply font weights to custom styles which are set as a `textAppearance` in XML on a TextView?如何将字体权重应用于在 TextView 上的 XML 中设置为“textAppearance”的自定义样式?
【发布时间】:2019-12-20 15:02:33
【问题描述】:

我说的是“细”、“常规”、“中”、“半粗”字体。不是粗体/斜体文本样式。 注意:我正在寻找一种通过 XML 以可重用样式应用字体粗细的方法。可以通过编程方式应用字体粗细(如本 Google 问题中所述:https://issuetracker.google.com/issues/145311058),但当布局资源中有数百个 TextView 时,这并不理想。

例如,我在应用主题级别设置了字体:

<style name="BaseTheme" parent="Theme.MaterialComponents.Light.DarkActionBar.Bridge">
   <item name="fontFamily">@font/rubik</item>
   <item name="android:fontFamily">@font/rubik</item>
   ...
</style>

<font-family xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
  <font
    android:font="@font/rubik_regular"
    android:fontStyle="normal"
    android:fontWeight="300"
    app:font="@font/rubik_regular"
    app:fontStyle="normal"
    app:fontWeight="300" />
  <font
    android:font="@font/rubik_italic"
    android:fontStyle="italic"
    android:fontWeight="300"
    app:font="@font/rubik_italic"
    app:fontStyle="italic"
    app:fontWeight="300" />
  <font
    android:font="@font/rubik_medium"
    android:fontStyle="normal"
    android:fontWeight="500"
    app:font="@font/rubik_medium"
    app:fontStyle="normal"
    app:fontWeight="500" />
  <font
    android:font="@font/rubik_medium_italic"
    android:fontStyle="italic"
    android:fontWeight="500"
    app:font="@font/rubik_medium_italic"
    app:fontStyle="italic"
    app:fontWeight="500" />
</font-family>

然后我将自定义样式应用于我的 TextViews 上的 textAppearance 属性:

<style name="SmallTextOnBackgroundMediumWeightTextView">
    <item name="android:textSize">@dimen/text_size_small</item>
    <item name="android:textColor">?attr/themeColorTextOnBackground</item>
    <item name="android:fontFamily">@font/rubik</item>
    <item name="fontFamily">@font/rubik</item>
    <item name="android:textFontWeight">500</item>
</style>

<TextView
    android:id="@+id/myTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="@style/SmallTextOnBackgroundMediumWeightTextView" />

注意:在此示例中,在我的自定义样式中设置 &lt;item name="android:textFontWeight"&gt;500&lt;/item&gt; 不会将中等粗细字体应用于我的 TextView。

我已尝试将我的样式中的android:fontFamilyfontFamily 项目显式设置为特定的中等字体@font/rubik_medium。这不起作用。

似乎唯一可行的方法是直接在 TextView 上显式设置 android:textStyle="bold",如果您有多个“更粗”的权重,这感觉不对,也不能解决问题。或者您可以直接在 TextView 上设置 android:textFontWeight="500",这违背了为 TextView 提供可重用自定义样式的目的,因为您将在每个……单个……TextView 上应用您的自定义样式 + 一个额外的 textFontWeight

有人可以帮我吗?如何重用我的自定义样式并在其中设置字体粗细?

【问题讨论】:

    标签: android android-layout android-styles


    【解决方案1】:

    请试试这个

    <style name="SmallTextOnBackgroundMediumWeightTextView">
        <item name="android:textSize">@dimen/text_size_small</item>
        <item name="android:textColor">?attr/themeColorTextOnBackground</item>
        <item name="android:fontFamily">@font/rubik</item>
        <item name="fontFamily">@font/rubik</item>
        <item name="android:textFontWeight">500</item>
        <item name="android:fontWeight">500</item>
        <item name="fontWeight">500</item>
    </style>
    

    在我测试之后 android:textFontWeight 需要 api 级别 28 和 android:fontWeight 需要 api 级别 26 希望对您有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-23
      • 1970-01-01
      • 2015-10-17
      • 2013-02-13
      • 2012-09-28
      • 2016-02-15
      • 2018-02-26
      相关资源
      最近更新 更多