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

无法覆盖AppTheme字体问题求助(附style.xml配置代码)

Fixing AppTheme Font Override Issues

Hey there, let’s work through why your AppTheme’s font family isn’t sticking. I’ve looked at your style configuration, and here are the most common fixes and checks to resolve this:

1. Verify Your Font Resource Setup

First, double-check that your font file is correctly placed and named:

  • Make sure montserrat_regular.ttf (or .otf) lives in the res/font/ directory (create the font folder if it doesn’t exist).
  • Android resource names are case-sensitive—ensure the filename is all lowercase (no spaces or uppercase letters) and matches exactly what you’ve referenced (@font/montserrat_regular).

2. Check for Style Hierarchy Conflicts

Sometimes other styles or theme settings can override your AppTheme’s font:

  • Confirm all your Activities in AndroidManifest.xml are using android:theme="@style/AppTheme"—no activity should have a separate theme specified that might take precedence.
  • If you’re using Material Components (Theme.MaterialComponents.* as your parent theme), note that some components use their own text appearance styles. You might need to override specific attributes like textAppearanceButton or textAppearanceHeadline6 in your AppTheme to apply the font to those components:
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Your existing color attributes -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        
        <!-- Base font family -->
        <item name="fontFamily">@font/montserrat_regular</item>
        <item name="android:fontFamily" tools:targetApi="jelly_bean">@font/montserrat_regular</item>
        
        <!-- Override component-specific text appearances if needed -->
        <item name="textAppearanceButton">@style/AppTextAppearance.Button</item>
    </style>
    
    <style name="AppTextAppearance.Button" parent="TextAppearance.AppCompat.Button">
        <item name="fontFamily">@font/montserrat_regular</item>
        <item name="android:fontFamily">@font/montserrat_regular</item>
    </style>
    

3. Use TextAppearance for Global Coverage

If setting fontFamily directly in AppTheme doesn’t apply to all controls, wrap it in a custom TextAppearance style and reference it in your theme:

<!-- Custom text appearance with your font -->
<style name="AppTextAppearance" parent="TextAppearance.AppCompat">
    <item name="fontFamily">@font/montserrat_regular</item>
    <item name="android:fontFamily">@font/montserrat_regular</item>
</style>

<!-- Update your AppTheme to use this text appearance -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Your color attributes -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    
    <!-- Apply text appearance globally -->
    <item name="textAppearance">@style/AppTextAppearance</item>
    <item name="android:textAppearance">@style/AppTextAppearance</item>
</style>

This ensures any component that inherits from the default text appearance will pick up your font.

4. Clean and Rebuild Your Project

Don’t overlook the basics—Android Studio’s build cache can sometimes cause resources to not update properly. Go to Build > Clean Project, then Build > Rebuild Project to rule out caching issues.

5. Advanced: Force Font via Custom Views (If All Else Fails)

If none of the above work, you can create a custom TextView that applies your font by default:

public class MontserratTextView extends AppCompatTextView {
    public MontserratTextView(Context context) {
        super(context);
        setupFont();
    }

    public MontserratTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setupFont();
    }

    public MontserratTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setupFont();
    }

    private void setupFont() {
        Typeface font = ResourcesCompat.getFont(getContext(), R.font.montserrat_regular);
        setTypeface(font);
    }
}

Then use this custom TextView in your layouts instead of the default TextView. You can also extend other components like Button or EditText the same way if needed.


内容的提问来源于stack exchange,提问作者Khemraj Sharma

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:01:36