【问题标题】:Prevent system FontSize change from affecting the size in the Xamarin Application防止系统 FontSize 更改影响 Xamarin 应用程序中的大小
【发布时间】:2017-04-08 06:03:13
【问题描述】:

所以我注意到,如果将用户手机中的字体大小更改为中等以上,我制作的应用程序看起来会很混乱,我在 Google 上搜索了一下,发现许多未回答或回答,但没有达到同一个问题的目的。 如果可能的话,我希望能够在 PCL 类中做到这一点,如果不可能,那么对我来说最有趣的平台是 android,所以针对 android 的修复就可以了。 这是我的 Xaml 代码示例,供您参考:

 <Label  Text="STORE" FontSize="23" HeightRequest="40" WidthRequest="212" VerticalTextAlignment="Center" HorizontalTextAlignment="Center"></Label>

所以要明确的问题是如何防止系统覆盖我在这种情况下为 23 的字体大小?

谢谢

【问题讨论】:

    标签: xaml xamarin xamarin.android portable-class-library


    【解决方案1】:

    对于那些仍然难以在 Android 上禁用无障碍字体缩放的用户。 您需要为标签、按钮和常用输入控件创建自定义渲染器,如下所示:

    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;
    
    [assembly: ExportRenderer(typeof(Label), typeof(MyApp.Droid.Renderers.LabelRendererDroid))]
    namespace MyApp.Droid.Renderers
    {
        class LabelRendererDroid : LabelRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
            {
                base.OnElementChanged(e);
                if (e.NewElement == null) return;
                Control.SetTextSize(Android.Util.ComplexUnitType.Dip, (float)e.NewElement.FontSize);
            }
        }
    }
    

    对于 Xamarin 选择器控件,没有 FontSize 属性,因此我们可以将其添加到 App 类中:

    public static double NormalFontSize => Device.GetNamedSize(NamedSize.Medium, typeof(Picker));
    

    然后在选择器渲染器中使用它:

    Control.SetTextSize(Android.Util.ComplexUnitType.Dip, (float)App.NormalFontSize);
    

    此外,通过更改此 NormalFontSize 属性,我们可以为选取器设置任何所需的字体大小,因为没有渲染器就无法使用它。

    【讨论】:

    • Gary McGhee 为标签和按钮 here 构建了精确的自定义渲染器类。此外,BrianLK 提供了在线程后面为 Entries 执行大部分工作的方法。祝你好运!
    【解决方案2】:

    只需将其添加到您的 MainActivity 中 OnCreate 之后。

            #region Font size change Prevent
            public override Resources Resources
            {
                get
                {
                    var config = new Configuration();
                    config.SetToDefaults();
                    return CreateConfigurationContext(config).Resources;
                }
            }
            #endregion Font size change Prevent
    

    【讨论】:

    • 当用户将字体大小设置得太大时,会出现字体大小溢出对话框的问题。这通过将 FontScale 默认重置为 1 解决了这个问题。
    • 这对在 pcl 中使用 app recourses 文件夹的任何人都有效。 (导致相同的命名空间)只需使用stackoverflow.com/questions/50289763/…
    • 谢谢!这解决了我的问题!字体大小现在尊重分配的值
    【解决方案3】:

    你看过本书第 5 章的内容吗? https://developer.xamarin.com/guides/xamarin-forms/creating-mobile-apps-xamarin-forms/

    虽然不是一个确切的答案,但根据您尝试执行的操作,您可能可以使用修改后的章节末尾的示例以允许“最大”字体大小。全部来自 PCL。

    值得注意的是,无法将字体缩放到大尺寸可能表明存在可访问性问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-28
      • 1970-01-01
      • 1970-01-01
      • 2014-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多