Android 开发者您好,
我目前正在开发一款 Android 应用程序,我热衷于确保它符合美国残疾人法案 (ADA),允许用户根据 Android 系统设置中设置的首选项缩放字体大小。然而,在适应喜欢超大字体的用户时,我遇到了挑战。
在某些设备上,最高字体大小设置的比例因子似乎设置为 1.3f,但在其他设备上,它似乎更高。这种差异会导致问题,因为当字体大小放大太多时,它会破坏应用程序的用户界面 (UI)。
我想在可访问性和维护用户界面的完整性之间取得平衡。是否有推荐的方法或最佳实践来设置 Android 应用程序中字体大小缩放的阈值?
这是我尝试过的示例代码:
override fun attachBaseContext(newBase: Context?) {
super.attachBaseContext(newBase)
val newOverride = Configuration(newBase?.resources?.configuration)
if(newOverride.fontScale > threshold) {
newOverride.fontScale = threshold
}
applyOverrideConfiguration(newOverride)
}
以下是我的一些具体问题:
如果有任何可以帮助我有效处理这种情况的见解、技巧或代码示例,我将不胜感激。预先感谢您的协助!
以下是修改代码的方法。 建议根据应用的设计和用户测试设置保守的阈值。我使用了 1.3f 因子,因为它对我来说是最好的。你可以玩玩它。
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(newBase);
adjustFontScalingThreshold();
}
private void adjustFontScalingThreshold() {
Configuration configuration = getResources().getConfiguration();
// Define your desired font scaling threshold here
float maxFontScaleThreshold = 1.3f;
// Check if font scaling is greater than the threshold
if (configuration.fontScale > maxFontScaleThreshold) {
configuration.fontScale = maxFontScaleThreshold;
// Create a new context with modified configuration
Context adjustedContext = createConfigurationContext(configuration);
// Apply the adjusted context as the base context
attachBaseContext(adjustedContext);
}
}
也只是为了给您更多提示和想法.. 您可以使用 DisplayMetrics 的 getScaledDensity() 方法来获取密度比例因子,该因子间接影响字体缩放。
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
float densityScaleFactor = displayMetrics.scaledDensity;
请注意,此因素与显示器的整体密度和缩放比例有关,并且可能与字体缩放阈值不直接对应。