如何避免Android系统设置修改字体时同步改变应用字体?
Got it, let's fix this annoying issue where your app's custom font (like ABC) gets overridden by the system's selected font (XYZ) when the user tweaks their system font settings. Here are reliable, actionable solutions to lock your app to its own font:
Method 1: Use Custom TextViews with Hardcoded Font
This is the most straightforward approach for individual text elements. Create a custom TextView class that loads your app's font on initialization, then use this class everywhere in your layouts instead of the default TextView.
import android.content.Context import android.graphics.Typeface import android.util.AttributeSet import androidx.appcompat.widget.AppCompatTextView class CustomFontTextView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : AppCompatTextView(context, attrs, defStyleAttr) { init { // Load your custom font from the assets folder val customFont = Typeface.createFromAsset(context.assets, "fonts/ABC.ttf") typeface = customFont } }
Then in your XML layouts:
<com.yourpackage.CustomFontTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This text stays as font ABC, no matter what system settings change"/>
Method 2: Apply Custom Font Globally via Base Activity
If you want to set this for your entire app without creating custom views, use a base activity that all your activities extend. This ensures that whenever the system font changes, your app re-applies its custom font automatically.
import android.view.View import android.view.ViewGroup import android.widget.TextView import android.content.res.Configuration import android.graphics.Typeface import android.os.Bundle import androidx.appcompat.app.AppCompatActivity open class BaseActivity : AppCompatActivity() { private lateinit var customFont: Typeface override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) customFont = Typeface.createFromAsset(assets, "fonts/ABC.ttf") applyCustomFontToAllViews(findViewById(android.R.id.content)) } override fun onConfigurationChanged(newConfig: Configuration) { super.onConfigurationChanged(newConfig) // Re-apply custom font when system configuration (including font) changes applyCustomFontToAllViews(findViewById(android.R.id.content)) } private fun applyCustomFontToAllViews(view: View?) { if (view is ViewGroup) { for (i in 0 until view.childCount) { applyCustomFontToAllViews(view.getChildAt(i)) } } else if (view is TextView) { view.typeface = customFont } } }
Method 3: Disable System Font Size Scaling (Optional)
If you also want to prevent system font size changes from messing with your app's layout, add this to your AndroidManifest.xml under the <application> tag or individual <activity> tags:
android:configChanges="fontScale|orientation|screenSize"
Then update your BaseActivity's onConfigurationChanged method to reset the font scale:
override fun onConfigurationChanged(newConfig: Configuration) { super.onConfigurationChanged(newConfig) // Force font scale back to 1.0 to ignore system font size adjustments val config = resources.configuration config.fontScale = 1.0f resources.updateConfiguration(config, resources.displayMetrics) // Re-apply custom font applyCustomFontToAllViews(findViewById(android.R.id.content)) }
Quick Notes for Jetpack Compose Users
If you're building with Compose, just set a fixed FontFamily in your MaterialTheme—it won't be affected by system font settings:
val customFontFamily = FontFamily( Font(R.font.abc_regular), Font(R.font.abc_bold, FontWeight.Bold) ) MaterialTheme( typography = Typography(defaultFontFamily = customFontFamily), // Other theme configurations ) { // Your app content goes here }
内容的提问来源于stack exchange,提问作者Rashid Afzaal

