【问题标题】:xamarin.forms (android ) Font scaling not workingxamarin.forms(android)字体缩放不起作用
【发布时间】:2020-03-09 19:12:14
【问题描述】:

无论用户的设置如何,我都想修复缩放。当用户更改显示设置并增加/减少字体大小时,我成功地完成了这项工作。

但我的应用在以下情况下不会将字体缩放为 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.

我希望在应用安装时字体应该缩放为 1。

【问题讨论】:

    标签: xamarin.forms xamarin.android


    【解决方案1】:

    您可以尝试覆盖 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);
    }
    

    【讨论】:

      【解决方案2】:

      由于不推荐使用 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;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-16
        • 1970-01-01
        • 2015-05-26
        • 1970-01-01
        • 2014-10-25
        • 1970-01-01
        相关资源
        最近更新 更多