Wenn Sie eine Schriftart für alle TextViews in Android-Anwendung festlegen müssen, können Sie diese Lösung verwenden. Es überschreibt ALLE TextView-Schriftarten, enthält eine Aktionsleiste und andere Standardkomponenten, aber die Passwortschriftart von EditText wird nicht überschrieben.
public class MyApp extends Application {
@Override
public void onCreate() {
TypefaceUtil.overrideFont(getApplicationContext(), "SERIF", "fonts/Roboto-Regular.ttf"); // font from assets: "assets/fonts/Roboto-Regular.ttf
}
}
Vergessen Sie nicht, android hinzuzufügen: name = "com.you.yourapp.MyApp" in Anwendung Tag der Manifest-Datei.
in Thema hinzufügen in style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyAppTheme" parent="@android:style/Theme.Holo.Light">
<!-- you should set typeface which you want to override with TypefaceUtil -->
<item name="android:typeface">serif</item>
</style>
</resources>
Erstellen Sie eine Klasse TypefaceUtil.java
import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import java.lang.reflect.Field;
public class TypefaceUtil {
/**
* Using reflection to override default typeface
* NOTICE: DO NOT FORGET TO SET TYPEFACE FOR APP THEME AS DEFAULT TYPEFACE WHICH WILL BE OVERRIDDEN
* @param context to work with assets
* @param defaultFontNameToOverride for example "monospace"
* @param customFontFileNameInAssets file name of the font from assets
*/
public static void overrideFont(Context context, String defaultFontNameToOverride, String customFontFileNameInAssets) {
try {
final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(), customFontFileNameInAssets);
final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride);
defaultFontTypefaceField.setAccessible(true);
defaultFontTypefaceField.set(null, customFontTypeface);
} catch (Exception e) {
Log.e("Can not set custom font " + customFontFileNameInAssets + " instead of " + defaultFontNameToOverride);
}
}
}
Hat das Thema angewandt? Sie können ein Theme auf die gesamte Anwendung anwenden, indem Sie 'android: theme =" @ style/AppTheme "' zum Feld ' ' in der Manifest.xml hinzufügen. –
Jack