当设备字体大小发生变化时,Android 不会增加应用程序中的文本

Dar*_*ony 2 android font-size

我有一个 Android 应用程序,在其中使用项目列表,例如左侧有一个缩略图,右侧有一些文本。

一切正常,但我注意到一个问题,当用户增加其设备的字体大小时(不是在应用程序中,但通常对于他的设备,因为他是老年人,需要更大的字母)。由于每个移动设备都可以在设置中更改字体大小 - 小。中型、大型、非常大等)

但是,此更改还增加了我的应用程序中的字体大小,然后项目列表看起来非常糟糕,因为图像右侧的文本变为多行,或者即使停留在一行上,缩放后看起来也很糟糕,因为设备文本大小增加了。

通常我使用 SP 中的文本大小(例如 16sp):

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="16sp"
</TextView>
Run Code Online (Sandbox Code Playgroud)

有没有办法让我的应用程序不受设备字体大小设置的影响,因为我的应用程序有自己的字体大小或者没有什么可做的?

现在,如果有人需要在他的设备上使用大字体,他就无法正常使用该应用程序。

Vas*_*iyT 5

您可以在基本活动中覆盖

override fun attachBaseContext(newBase: Context?) {
    super.attachBaseContext(newBase)
    val config = Configuration(newBase?.resources?.configuration)
    config.fontScale = 1.0f
    applyOverrideConfiguration(config)
}
Run Code Online (Sandbox Code Playgroud)

爪哇:

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(newBase);
    Configuration config = new Configuration(newBase.getResources().getConfiguration());
    config.fontScale = 1.0f;
    applyOverrideConfiguration(config);
}
Run Code Online (Sandbox Code Playgroud)