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

Android Studio中如何修改XML文件内按钮的字体?

Troubleshooting Button Font Family Not Working in Android Studio

Hey there, let's work through why your Titilum Bold font isn't showing up on your button. Here are some practical fixes to try out:

  • Double-check spelling and filename matching
    You mentioned your font is named Tilitium Bold, but your XML uses @font/Titilum-Bold—spot the typo? "Tilitium" vs "Titilium" is a tiny difference that breaks everything. Android is case-sensitive with resource references, so make sure the exact filename (including hyphens, capitalization, and no spaces) matches your code. For example, if your font file is Tilitium-Bold.ttf, your reference should be @font/Tilitium-Bold.

  • Confirm the font folder structure
    Ensure your font file lives directly in res/font/—not res/fonts/ or any other subfolder. Android Studio automatically recognizes the font directory as the correct place for custom fonts, so a misnamed folder will prevent the system from picking up your file.

  • Validate the font file itself
    Corrupted or unsupported font files won't load properly. Try opening the TTF/OTF file on your computer first—if it doesn't display correctly there, grab a fresh copy of the font. Also, avoid special characters in the filename; stick to letters, numbers, hyphens, and underscores.

  • Test setting the font programmatically
    If the XML attribute isn't working, bypass it and set the font in your Activity/Fragment code to rule out styling conflicts:

    val targetButton = findViewById<Button>(R.id.your_button_id)
    val boldTypeface = ResourcesCompat.getFont(this, R.font.Tilitium_Bold)
    targetButton.typeface = boldTypeface
    

    (Adjust to Java syntax if that's what you're using.)

  • Check for style overrides
    Your button might be using a style that's overriding the fontFamily attribute. Open your button's style (either in the layout XML or styles.xml) and make sure there's no conflicting fontFamily or textAppearance setting taking priority. You can also try adding android:fontFamily directly to the button's style instead of the layout element itself.

  • Clean and rebuild your project
    Android Studio's build cache can cause odd resource loading issues. Go to Build > Clean Project, then Build > Rebuild Project, and run your app again. This clears any cached data that might be blocking your font from loading.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:54:59