我一直在为我的应用程序中的所有视图使用自定义字体。但目前该字体仅适用于工具栏,而其余视图使用默认字体。这是因为appcompat lib更新到版本23.4.0.1的副作用。有没有人遇到过这个问题。请在下面找到字体用法类
FontUtility.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Graphics;
namespace HPScanJA.Android.Utils
{
class FontUtility
{
public static Typeface applyRegularFont(Context context)
{
Typeface hpRegularTypeface = Typeface.CreateFromAsset(context.Assets, "fonts/Simplified_Rg.ttf");
return hpRegularTypeface;
}
public static Typeface applyBoldFont(Context context)
{
Typeface hpBoldTypeface = Typeface.CreateFromAsset(context.Assets, "fonts/Simplified_Bd.ttf");
return hpBoldTypeface;
}
}
}
用法也在下面显示的基本活动中。这将通过所有活动得到扩展
BaseActivity.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Graphics;
using HPScanJA.Android.Utils;
using Android.Support.V7.App;
namespace HPScanJA.Android.Activities
{
[Activity(Label = "BaseActivity")]
public class BaseActivity : AppCompatActivity
{
Context context;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
context = this;
// Get the Resources object from our context
global::Android.Content.Res.Resources res = context.Resources;
// Create your application here
int titleId = res.GetIdentifier("action_bar_title", "id",
"android");
TextView titleView = (TextView)FindViewById(titleId);
if (titleView != null)
titleView.SetTypeface(FontUtility.applyRegularFont(context), TypefaceStyle.Normal);
}
public override View OnCreateView(string name, Context context, global::Android.Util.IAttributeSet attrs)
{
View view = base.OnCreateView(name, context, attrs);
return setCustomTypeFaceIfNeeded(name, attrs, view);
}
protected View setCustomTypeFaceIfNeeded(String name, global::Android.Util.IAttributeSet attrs, View view)
{
View result = null;
Console.WriteLine("*******************************************"+name+"*******************************************");
if ("TextView".Equals(name))
{
result = new TextView(this, attrs);
if (null != ((TextView)result).Typeface && TypefaceStyle.Bold == ((TextView)result).Typeface.Style)
{
((TextView)result).SetTypeface(FontUtility.applyBoldFont(context), TypefaceStyle.Normal);
}
else
{
((TextView)result).SetTypeface(FontUtility.applyRegularFont(context), TypefaceStyle.Normal);
}
}
else if ("EditText".Equals(name))
{
result = new EditText(this, attrs);
if (null != ((EditText)result).Typeface && TypefaceStyle.Bold == ((EditText)result).Typeface.Style)
{
((EditText)result).SetTypeface(FontUtility.applyBoldFont(context), TypefaceStyle.Normal);
}
else
{
((EditText)result).SetTypeface(FontUtility.applyRegularFont(context), TypefaceStyle.Normal);
}
}
else if ("Button".Equals(name))
{
result = new Button(this, attrs);
if (null != ((Button)result).Typeface && TypefaceStyle.Bold == ((Button)result).Typeface.Style)
{
((Button)result).SetTypeface(FontUtility.applyBoldFont(context), TypefaceStyle.Normal);
}
else
{
((Button)result).SetTypeface(FontUtility.applyRegularFont(context), TypefaceStyle.Normal);
}
}
else if ("CheckedTextView".Equals(name))
{
result = new CheckedTextView(this, attrs);
if (null != ((CheckedTextView)result).Typeface && TypefaceStyle.Bold == ((CheckedTextView)result).Typeface.Style)
{
((CheckedTextView)result).SetTypeface(FontUtility.applyBoldFont(context), TypefaceStyle.Normal);
}
else
{
((CheckedTextView)result).SetTypeface(FontUtility.applyRegularFont(context), TypefaceStyle.Normal);
}
}
else if ("CheckBox".Equals(name))
{
result = new CheckBox(this, attrs);
if (null != ((CheckBox)result).Typeface && TypefaceStyle.Bold == ((CheckBox)result).Typeface.Style)
{
((CheckBox)result).SetTypeface(FontUtility.applyBoldFont(context), TypefaceStyle.Normal);
}
else
{
((CheckBox)result).SetTypeface(FontUtility.applyRegularFont(context), TypefaceStyle.Normal);
}
}
else if ("RadioButton".Equals(name))
{
result = new RadioButton(this, attrs);
if (null != ((RadioButton)result).Typeface && TypefaceStyle.Bold == ((RadioButton)result).Typeface.Style)
{
((RadioButton)result).SetTypeface(FontUtility.applyBoldFont(context), TypefaceStyle.Normal);
}
else
{
((RadioButton)result).SetTypeface(FontUtility.applyRegularFont(context), TypefaceStyle.Normal);
}
}
else if ("Switch".Equals(name))
{
result = new Switch(this, attrs);
if (null != ((Switch)result).Typeface && TypefaceStyle.Bold == ((Switch)result).Typeface.Style)
{
((Switch)result).SetTypeface(FontUtility.applyBoldFont(context), TypefaceStyle.Normal);
}
else
{
((Switch)result).SetTypeface(FontUtility.applyRegularFont(context), TypefaceStyle.Normal);
}
}
if (result == null)
{
return view;
}
else
{
return result;
}
}
}
}
如果有人遇到这个问题,请告诉我。
答案 0 :(得分:0)
过了一会儿终于我解决了这个...... 当BaseActivity类尝试在运行时更改字体时,会发生此问题。在支持库v23之前,这工作正常。但是从支持lib v23开始,系统在运行时更改了某些视图类型,因此这段代码无效。
我通过使用不同的方法解决了这个问题,即扩展每个UI View类并在其中设置字体 例如。 Textviews代码如下所示
public class CustomTextView : TextView
{
public CustomTextView(Context context) : base(context)
{
SetCustomTypeFace();
}
public CustomTextView(Context context, IAttributeSet attrs) : base(context, attrs)
{
SetCustomTypeFace();
}
public CustomTextView(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
{
SetCustomTypeFace();
}
public CustomTextView(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
{
SetCustomTypeFace();
}
public void SetCustomTypeFace()
{
Typeface tf = null;
if (this.Typeface.Style == TypefaceStyle.Normal)
{
tf = Typeface.CreateFromAsset(Application.Context.Assets, "fonts/Simplified_Rg.ttf");
}
else if (this.Typeface.Style == TypefaceStyle.Bold)
{
tf = Typeface.CreateFromAsset(Application.Context.Assets, "fonts/Simplified_Bd.ttf");
}
SetTypeface(tf, this.Typeface.Style);
}
}
然后我在布局中使用了这个自定义textview而不是默认的TextView。