Kur*_*s92 6 android font-family android-fonts typeface android-5.0-lollipop
我想在我的应用程序中将sans-serif light设置为默认字体.我正在使用Android Lollipop设备.所以,这是我的styles.xml:
<resources>
<style name="AppBaseTheme" parent="android:Theme.Material.Light.DarkActionBar">
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:textViewStyle">@style/RobotoTextViewStyle</item>
<item name="android:buttonStyle">@style/RobotoButtonStyle</item>
</style>
<style name="RobotoTextViewStyle" parent="android:Widget.TextView">
<item name="android:fontFamily">sans-serif-light</item>
</style>
<style name="RobotoButtonStyle" parent="android:Widget.Button">
<item name="android:fontFamily">sans-serif-light</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
当我在我的设备上运行应用程序时,在每个视图中都不应用sans-serif-light.例如,ActivityMain.java中的TextViews显示我想要的字体,但在其他活动中,如SecondActivity.java,所有TextView都会正常显示.如果我在使用Android 4.1的设备上运行我的应用程序,它适用于每个视图.我究竟做错了什么?提前致谢 :)
如果使用 Material Design 主题对您来说不重要,您可以使用:
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:typeface">sans-serif-light</item>
</style>
Run Code Online (Sandbox Code Playgroud)
如果使用 Material 主题对您的应用程序很重要,您可以使用以下技术:
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, "SANS_SERIF", "sans_serif_light.ttf");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1533 次 |
| 最近记录: |