0

Ich habe eine benutzerdefinierte Schriftart für alle Ansichten in meiner Anwendung verwendet. Derzeit wird die Schriftart jedoch nur für die Symbolleiste angewendet, während der Rest der Ansichten die Standardschriftart verwendet. Dies geschah als Nebeneffekt eines appcompat lib-Updates auf Version 23.4.0.1. Hat jemand dieses Problem erlebt? Nachstehend finden Sie die Schrift NutzungsklassenBenutzerdefinierte Schriftart funktionierte nicht nach dem Aktualisieren der appcompat-Bibliothek auf 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; 
     } 
    } 
} 

auch die Nutzung ist in der Basisaktivität weiter unten. Dies wird von allen Aktivitäten

erweitert

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


    } 
} 

Bitte lassen Sie mich wissen, ob jemand dieses Problem konfrontiert.

+0

Was ist das: 23.4.0.1, das ist nicht möglich .. nur 23.4.0 verwenden nur .. entfernen 1 –

+0

dies ist die aktualisierte Version der Android-Support-Bibliothek. Auch das ist nichts, was ich am Ende hinzugefügt habe. Die Versionsnummer wurde zusammen mit dem Update –

+0

angezeigt, aber die neueste aktualisierte Bibliothek ist 24.1.1 .. Sie können es hier finden .. http://digleplease.appspot.com/, sie zeigen gerade eine stabile Version .. nd immer Verwenden Sie stabile Version für Projekt –

Antwort

0

Nach einer langen Zeit löste ich dies ... Dieses Problem trat auf, als die BaseActivity-Klasse versucht, die Schriftart während der Laufzeit zu ändern. Vor der Unterstützung der Bibliothek v23 funktionierte das gut. Aber ab der Support-Bibliothek v23 änderte das System bestimmte Ansichtsarten während der Laufzeit und daher funktionierte dieses Code-Stück nicht.

Ich löste dies, indem ich mit einem anderen Ansatz ging, der jede UI View-Klasse erweitert und die Schrift darin einstellt Für z. der TextViews-Code ist unten angegeben

Dann habe ich diese benutzerdefinierte Textansicht im Layout anstelle der Standardtextansicht verwendet.