将 appcompat 库更新到 23.4.0.1 后自定义字体不起作用

Custom font not working after updating appcompat library to 23.4.0.1

我一直在为我的应用程序中的所有视图使用自定义字体。但目前该字体仅适用于工具栏,而其余视图使用默认字体。这发生在 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;
        }
    }
}

用法也在下面显示的基础 activity 中。这得到了所有活动的扩展

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;
            }
        }


    }
}

如果有人遇到这个问题,请告诉我。

折腾了好久终于解决了... 此问题是在 BaseActivity class 尝试在 运行 期间更改字体时发生的。在支持库 v23 之前,这工作正常。但是从支持 lib v23 开始,系统在 运行 时间内更改了某些视图类型,因此这段代码无法正常工作。

我通过一种不同的方法解决了这个问题,即扩展每个 UI 视图 class 并在其中设置字体 例如。下面给出了 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。