为什么我的自定义字体在Android应用程序发布模式下被默认字体替换?

3
当我以Debug模式构建和部署包含自定义字体的Xamarin.android应用程序时,一切正常。我可以在应用程序中看到自定义ttf字体。但是,当我以Release模式构建和部署相同的应用程序时,运行它时我会看到默认字体。是否需要添加某些必需权限才能使自定义字体在应用程序中可见?
以下是我在应用程序中使用字体的方式:
  1. Convert method in converter class:

    protected override Typeface Convert(string fontName, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        try
        {
            if (!fontName.StartsWith(@"font/")) fontName = @"font/" + fontName;
            if (!fontName.EndsWith(".ttf")) fontName += ".ttf";
    
            if (!_cache.ContainsKey(fontName))
            {
                _cache[fontName] = Typeface.CreateFromAsset(Application.Context.Assets, fontName);
            }
    
            return _cache[fontName];
        }
        catch (Exception e)
        {
            Android.Util.Log.Error("AndroidFont", e.ToString());
    
            return Typeface.Default;
        }
    }
    
  2. Item in AXML

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"              
              >
    <TextView
      android:layout_width="180dp"
      android:layout_height="wrap_content"
      android:textColor="#AAAAAA"
      local:MvxBind="Typeface StringToFont('Lato'), Converter=StringToFontConverter" /></LinearLayout>
    
  3. Registering converter in Setup.cs

    protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
    {
        registry.RegisterFactory(new MvxCustomBindingFactory<View>("StringToFont", view => new BackgroundBinding(view)));
    }
    

TTF字体位于Assets / fonts目录中。我使用的是Visual Studio Community 2015(C#)。

在Debug模式下一切正常,但当我切换到Release模式时,我的字体(Lato)被默认的Android字体替换了。为什么?

1个回答

1
  1. Create MvxViewFontBinding

    public abstract class MvxViewFontBinding : MvxAndroidTargetBinding
    {
        protected TextView TextView => (TextView)base.Target;
        public MvxViewFontBinding(TextView view) : base(view)
        {
        }
    
        public override MvxBindingMode DefaultMode => MvxBindingMode.OneWay;
    
        public override Type TargetType => typeof(Android.Graphics.Typeface);
    }
    
  2. Create MvxViewCustomFontBinding

    public class MvxViewCustomFontBinding : MvxViewFontBinding
    {
        public MvxViewCustomFontBinding(TextView view) : base(view)
        {
        }
    
        protected override void SetValueImpl(object target, object value)
        {
            var textView = (TextView)target;
            textView?.SetTypeface((Android.Graphics.Typeface)value, Android.Graphics.TypefaceStyle.Normal);
        }
    }
    
  3. Registering in Setup.cs

    protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
    {
        registry.RegisterFactory(new MvxCustomBindingFactory<TextView>("CustomFont", textView => new MvxViewCustomFontBinding(textView)));
        base.FillTargetFactories(registry);
    }
    
  4. You can bind it on axml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:local="http://schemas.android.com/apk/res-auto"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff">
    <TextView
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:textColor="#AAAAAA"
        local:MvxBind="CustomFont StringToFont('Lato')" /></LinearLayout>
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接