You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XML设置TextAppearance后,动态设置自定义字体失效问题求助

Hey Felipe! Let's break down why your custom font isn't working when using textAppearance in your XML, and walk through some solutions to fix this.

The Root Cause

The issue here is likely that your Paragraph3 style includes a fontFamily or typeface attribute. When you set textAppearance on the TextView, it applies all the style properties defined in that style—including the font—and this can override the typeface you're setting dynamically in your extension method. Even if you set the typeface in code, the text appearance's font setting might take precedence (or get reapplied after your code runs in some cases).

Solutions

1. Remove Font Declarations from Your Text Appearance Style

The simplest fix is to edit your Paragraph3 style and remove any fontFamily or typeface attributes. This way, the text appearance only controls other text properties (like size, color, line height) and leaves the font to be set dynamically via your extension method.

Example of the modified style:

<style name="Paragraph3">
    <!-- Keep other properties like textSize, textColor, etc. -->
    <item name="android:textSize">16sp</item>
    <item name="android:textColor">@color/black</item>
    <!-- Remove any fontFamily/typeface lines here -->
</style>

2. Adjust Your Extension Method to Force Override

If you need to keep the font declaration in Paragraph3, you can modify your extension method to ensure it properly overrides the text appearance's font. Using TextViewCompat.setTypeface instead of directly setting the typeface property can help with compatibility and ensure the font is applied correctly:

fun TextView.setCustomFont(fontResId: Int) {
    val typeface = ResourcesCompat.getFont(context, fontResId)
    TextViewCompat.setTypeface(this, typeface)
}

Alternatively, you can explicitly set the fontFamily property (with backward compatibility handled by TextViewCompat):

fun TextView.setCustomFont(fontResId: Int) {
    val font = ResourcesCompat.getFont(context, fontResId)
    TextViewCompat.setFontFamily(this, font)
}

3. Ensure Your Extension Method is Called at the Right Time

Double-check that you're calling setCustomFont after the TextView has been fully initialized. For example:

  • In an Activity, call it in onCreate after setContentView
  • In a Fragment, call it in onViewCreated after the view is inflated
  • If using data binding/view binding, make sure the binding is fully initialized before invoking the method

Final Notes

If you're still stuck, inspect your Paragraph3 style to confirm it doesn't inherit font settings from a parent style. If it does, add <item name="android:fontFamily">@null</item> to Paragraph3 to explicitly clear inherited font declarations.

内容的提问来源于stack exchange,提问作者Felipe Castilhos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:00:21