Android Studio中如何修改XML文件内按钮的字体?
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 isTilitium-Bold.ttf, your reference should be@font/Tilitium-Bold.Confirm the font folder structure
Ensure your font file lives directly inres/font/—notres/fonts/or any other subfolder. Android Studio automatically recognizes thefontdirectory 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 thefontFamilyattribute. Open your button's style (either in the layout XML orstyles.xml) and make sure there's no conflictingfontFamilyortextAppearancesetting taking priority. You can also try addingandroid:fontFamilydirectly 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 toBuild > Clean Project, thenBuild > Rebuild Project, and run your app again. This clears any cached data that might be blocking your font from loading.
内容的提问来源于stack exchange,提问作者MikisaMaliDojnik

