问题描述
是否可以在Android应用程序中设置自定义字体?
Is it possible to set a custom font in an Android application?
我尝试在此处上发布了什么内容,但我不知道我的extends Application
类在哪里...
I tried what is posted here, but I don't know where my extends Application
class is...
有帮助吗?
我尝试了以下操作:
- 添加资产文件夹并在其中插入字体,如下所示:
-
添加一个从
Application
从我的AndroidManifest.xml
调用此新课程.
我去了我的风格并将其添加.
I went to my style and added it.
MyApp.java:
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
FontsOverride.setDefaultFont(this, "DEFAULT", "raleway_regular.ttf");
// This FontsOverride comes from the example I posted above
}
}
AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:name=".MyApp"
android:theme="@style/AppTheme">
....
styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:fontFamily">default</item>
</style>
但是我的字体仍然没有变化...知道吗?
But my font is still not changning... any idea?
然后调用MyApp
类.但是对我的字体没有影响...
Then the MyApp
class is called. But no effect on my fonts...
EDIT2 :我意识到在为按钮设置自定义样式后,按钮会应用自定义字体.这是我的自定义按钮样式:
I realized that my buttons apply the custom font after I set a custom style for my buttons. Here is my custom button style:
<style name="MyButtonStyle" parent="Widget.AppCompat.Button">
<item name="textAllCaps">false</item>
<item name="android:textAllCaps">false</item>
</style>
这是现在的样子:
所以:我的按钮正在应用样式,而不是TextView
.为什么我的自定义字体不能应用于应用程序中的所有项目?
So: my button is applying the style, but not the TextView
. Any idea on why my custom font is not being applied for all items in application?
推荐答案
写一个类
public class MyApp extends Application{
// Put the onCreate code as you obtained from the post link you reffered
}
现在,接下来是在AndroidManifest.xml中,应用程序标记为您的应用程序类提供名称.在这种情况下是MyApp
now next thing is in AndroidManifest.xml for the application tag give name for your application class. In this case it is MyApp
<application
android:name=".MyApp"
...
>
...
</application>
因此,每当打开App时,都会调用MyApp类的onCreate方法,并设置字体.
So whenever the App is opened , onCreate method of MyApp class would be invoked , and the font would be set.
更新将字体文件放在assets/fonts/your_font_file.ttf下
UpdatePut font file under assets/fonts/your_font_file.ttf
将此行放在您的应用程序类(MyApp)的onCreate方法下
Put this line under onCreate method of your application class(MyApp)
TypefaceUtil.overrideFont(getApplicationContext(), "SERIF", "fonts/your_font_file.ttf");
TypefaceUtil的源文件
Source File for TypefaceUtil
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) {
final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(), customFontFileNameInAssets);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Map<String, Typeface> newMap = new HashMap<String, Typeface>();
newMap.put("serif", customFontTypeface);
try {
final Field staticField = Typeface.class
.getDeclaredField("sSystemFontMap");
staticField.setAccessible(true);
staticField.set(null, newMap);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} else {
try {
final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride);
defaultFontTypefaceField.setAccessible(true);
defaultFontTypefaceField.set(null, customFontTypeface);
} catch (Exception e) {
Log.e(TypefaceUtil.class.getSimpleName(), "Can not set custom font " + customFontFileNameInAssets + " instead of " + defaultFontNameToOverride);
}
}
}
}
现在更新您的style.xml文件
Now update your style.xml file
将下面的行设置为清单文件中用于您的活动的样式
put the below line your style which is included for your activity in manifest file
<item name="android:typeface">serif</item>
希望这会有所帮助
这篇关于如何在Android中为整个应用程序设置自定义字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!