当我在调试模式下构建和部署包含自定义字体的Xamarin.android应用程序时,一切正常。我可以在应用程序中看到自定义ttf字体。 但是,当我在发布模式下构建和部署相同的应用程序时,当我运行它时,我看到它的默认字体。 是否有一些必需的权限应该在应用属性中添加,以便自定义字体在应用中可见?
以下是我在应用中使用字体的方式:
转换器类中的转换方法:
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;
}
}
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>
在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#)。
一切正常,但仅在调试模式下。当我切换到发布模式时,我的字体(Lato)被默认的Android字体替换。为什么呢?
答案 0 :(得分:1)
创建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);
}
创建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);
}
}
在Setup.cs中注册
protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
registry.RegisterFactory(new MvxCustomBindingFactory<TextView>("CustomFont", textView => new MvxViewCustomFontBinding(textView)));
base.FillTargetFactories(registry);
}
您可以将其绑定在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>