import java.lang.reflect.Field;
import android.content.Context;
import android.graphics.Typeface;
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);
}
protected 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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
将此类添加到您的代码中。
public final class Application extends android.app.Application {
@Override
public void onCreate() {
super.onCreate();
FontsOverride.setDefaultFont(this, "DEFAULT", "fonts/GeezEdit.ttf");
FontsOverride.setDefaultFont(this, "MONOSPACE", "fonts/GeezEdit.ttf");
/*FontsOverride.setDefaultFont(this, "MONOSPACE", "MyFontAsset2.ttf");
FontsOverride.setDefaultFont(this, "SERIF", "MyFontAsset3.ttf");
FontsOverride.setDefaultFont(this, "SANS_SERIF", "MyFontAsset4.ttf");*/
}
}
Run Code Online (Sandbox Code Playgroud)
.....在这里您可以看到已添加了一些字体/字体名称。这些是外部字体文件,可用于覆盖键盘视图/标签。
将此应用程序名称添加到android清单文件中的应用程序名称
例
<application
android:name=".Application"
android:allowBackup="false"
android:installLocation="internalOnly"
android:label="@string/ime_name"
android:theme="@style/AppTheme" >.......
Run Code Online (Sandbox Code Playgroud)
现在将上面覆盖的字体名称更新为您的样式。基本主题或清单应用程序中使用的主题。
例
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:typeface">monospace</item>
</style>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4284 次 |
| 最近记录: |