无论用户的设置如何,我都希望进行修复缩放。当用户更改显示设置并增加/减小字体大小时,我可以成功进行此操作。
但是在以下情况下,我的应用程序无法将字体缩放为1:
我尝试通过更新/覆盖配置来进行字体缩放。
private void AdjustFontScale()
{
Configuration configuration = Resources.Configuration;
var fontScale = configuration.FontScale;
configuration.FontScale = (float)1;
DisplayMetrics metrics = new DisplayMetrics();
WindowManager.DefaultDisplay.GetMetrics(metrics);
metrics.ScaledDensity = configuration.FontScale * metrics.Density;
BaseContext.Resources.UpdateConfiguration(configuration, metrics);
}
- I have added android:configChanges="fontScale" in my androidmanifest.xml file.
- AdjustFontScale() is called from OnCreate(), OnResume() and from OnConfigurationChanged() functions.
我希望在App安装时将字体缩放为1。
答案 0 :(得分:0)
您可以尝试覆盖 attachBaseContext 方法:
protected override void AttachBaseContext(Context @base)
{
Configuration overrideConfiguration = new Configuration();
overrideConfiguration = @base.Resources.Configuration;
overrideConfiguration.SetToDefaults();
var fontScale = overrideConfiguration.FontScale;
overrideConfiguration.FontScale = (float)1;
Context context = @base.CreateConfigurationContext(overrideConfiguration);
base.AttachBaseContext(context);
}
答案 1 :(得分:0)
答案 2 :(得分:0)
由于不赞成使用UpdateConfiguration,因此您现在应该使用此变体。支持最小和最大字体大小。 Xamarin.Essential需要访问显示密度。
public void AdjustFontScale()
{
var minFontScale = 0.9f;
var maxFontScale = 1.3f;
Configuration configuration = Resources.Configuration;
configuration.FontScale = Math.Max(minFontScale, Math.Min(maxFontScale, configuration.FontScale));
ApplicationContext.Resources.Configuration.UpdateFrom(configuration);
BaseContext.Resources.Configuration.UpdateFrom(configuration);
BaseContext.Resources.DisplayMetrics.ScaledDensity = configuration.FontScale * (float)DeviceDisplay.MainDisplayInfo.Density;
}