2017-06-10 3 views
0

ich eine benutzerdefinierte Schriftart für alle Textviews in My app.I festlegen möchten unten Code verwendetwie in Lutscher benutzerdefinierte Schriftart für gesamte Anwendung setzen

public static void overrideFont(Context context, String defaultFontNameToOverride, String customFontFileNameInAssets) 
    { 
     try 
     { 
      final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(),customFontFileNameInAssets); 
      Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride); 
      defaultFontTypefaceField.setAccessible (true); 
      defaultFontTypefaceField.set(null,customFontTypeface); 
     } 
     catch (Exception e) 
     { 
      Log.e("getMessage",e.getMessage()); 
      e.printStackTrace(); 
      //Log.e("Can not set custom font " + customFontFileNameInAssets + " instead of " + defaultFontNameToOverride); 
     } 

    } 

und rufen Methode wie unten

TypefaceUtil.overrideFont(this, "DEFAULT", getTypeFace()); 
     TypefaceUtil.overrideFont(this, "MONOSPACE", getTypeFace()); 
     TypefaceUtil.overrideFont(this, "SERIF", getTypeFace()); 
     TypefaceUtil.overrideFont(this, "SANS_SERIF", getTypeFace()); 

dieser Code funktioniert in Android vor android 5 gut, aber es funktioniert nicht in Lollipop. Ich suche viel, aber ich finde keine Dinge.

+0

Sie sollten auch wie implementieren '' –

Antwort

3

Ich habe tat dies mit Calligraphy

in Ihrer Anwendungsklasse in der #onCreate() -Methode.

@Override 
public void onCreate() { 
    super.onCreate(); 
    CalligraphyConfig.initDefault(new CalligraphyConfig.Builder() 
          .setDefaultFontPath("fonts/Roboto-RobotoRegular.ttf") 
          .setFontAttrId(R.attr.fontPath) 
          .build() 
      ); 
    //.... 
} 

Wickeln Sie die Aktivität Kontext:

@Override 
protected void attachBaseContext(Context newBase) { 
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase)); 
} 
+0

dank Code in Aktivitäten sehr gut, aber in Dialogen funktioniert es nicht.eine Idee? –

+0

@zohrehkh, wenn Sie eine Basisaktivität haben und die Methode attachTheBaseContext implementiert haben. Es sollte für jede Aktivität funktionieren, die BaseActivity erbt –

+0

Es funktioniert nicht für Ansichten, die in Code nur über XML erstellt werden. – user3690202

0

Angenommen, Ihre gewählte Schriftart serif-monospace ist, dann implementieren Sie sollten auch wie

<style name="TextView" parent="android:Widget.TextView"> 
     <item name="android:fontFamily">serif-monospace</item> 
    </style> 

und verwenden wie dieses

<TextView 
    style="@style/TextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 
2

Ich habe es Schriftarten durch zwingende Anwendung standardmäßig durchgeführt:

Erstellen Sie eine Klasse FontsOverride:

public final class FontsOverride { 

public static void setDefaultFont(Context context, 
            String staticTypefaceFieldName, String fontAssetName) { 
    final Typeface regular = Typeface.createFromAsset(context.getAssets(), 
      fontAssetName); 
    replaceFont(staticTypefaceFieldName, regular); 
} 

private static void replaceFont(String staticTypefaceFieldName, 
           final Typeface newTypeface) { 
    try { 
     final Field staticField = Typeface.class 
       .getDeclaredField(staticTypefaceFieldName); 
     staticField.setAccessible(true); 
     staticField.set(null, newTypeface); 
    } catch (NoSuchFieldException e) { 
     e.printStackTrace(); 
    } catch (IllegalAccessException e) { 
     e.printStackTrace(); 
    } 
    } 
} 

eine Application Klasse erstellen oder es zu Ihrem aktuellen hinzufügen, wenn Sie bereits haben:

public class AppController extends Application { 

// add a folder named `fonts` under `assets` folder 
// add theme here 
String customFont1 = "fonts/IRANSans-Light-web.ttf"; 
String customFont2 = "fonts/font2.ttf"; 
String customFont3 = "fonts/font3.ttf"; 
String customFont4 = "fonts/font4.ttf"; 

@Override 
public void onCreate() { 
    super.onCreate(); 
    // override default fonts 
    FontsOverride.setDefaultFont(this, "DEFAULT", customFont1); 
    FontsOverride.setDefaultFont(this, "MONOSPACE", customFont2); 
    FontsOverride.setDefaultFont(this, "SERIF", customFont3); 
    FontsOverride.setDefaultFont(this, "SANS_SERIF", customFont4); 

} 

}

In Schrift auf Ihrem Thema in style

<!-- Application theme --> 
<style name="myTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="android:typeface">monospace</item> 
</style> 

Und schließlich fügen Sie sie manifest in Application tag:

<application 
    android:name="path.AppController" 
    android:icon="@mipmap/ic_launcher" 
    android:theme="@style/myTheme" 
    android:label="@string/app_name"> 

(ich habe es getestet und es funktioniert auf `Lolipop auch)

0

Ich machte eine rekursive Funktion und behielt diese in meinem BaseActivity:

Fontutils:

public abstract class FontUtils { 

    public static Typeface regular, bold; 

    public static Typeface getFont(Context appContext) { 
    if (regular == null) { 
     regular = Typeface.createFromAsset(appContext.getAssets(), "fonts/Lato-Regular.ttf"); 
    } 
    return regular; 
    } 


    public static Typeface getFontBold(Context appContext) { 
    if (bold == null) { 
     bold = Typeface.createFromAsset(appContext.getAssets(), "fonts/Lato-Bold.ttf"); 
    } 
    return bold; 
    } 
} 

Sie können Ihre eigenen Fälle hinzufügen plus i verwenden das Tag in der XML für fett und regelmäßige Schriftarten

android:tag="bold" 

Rufen Sie die Methode in onCreate des BaseActivity wie folgt aus:

View contentView = this.findViewById(android.R.id.content); 
overrideFonts(this, contentView); 

Stellen Sie sicher, dass Sie in Ihrer Aktivität den Befehl super.OnCreate nach setContentView aufrufen.

Hoffe, das hilft.

0

Überprüfen Sie diese: Working with fonts.

Von der I/O 2017 können Sie jetzt die folgenden in xml verwenden:

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:fontFamily="@font/lobster"/> 

Google diese Unterstützung für A-Geräte hinzugefügt, aber auch, ich zitiere:

Die Support Library 26.0 Beta bietet Unterstützung für die Funktion "Schriftarten in XML" auf Geräten mit Android API Version 14 und höher.

Verwandte Themen