问题
Hello I want to set same font family to entire Android application. I have seen this Is it possible to set a custom font for entire of application? but this all doing using code. I want to set in Theme
such that it will work in all. It should convert the font family of ActionBar
text also.
Any way to do this ?
styles.xml
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
回答1:
If you want to set the TypeFace for all of your Views without having to do it programatically each time (and still work across all versions of Android), your best option would be to subclass the View and have it automatically set the TypeFace you want.
IE.
public class CustomTextView extends TextView{
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomTextView(Context context) {
super(context);
init();
}
public void init(boolean bold) {
setTypeface(Typeface.createFromAsset(getAssets(), "fonts/your_font_file.ttf"));
}
}
If you want to really optimize that even further, you can use a static reference to that TypeFace and use that so you don't need to recreate the TypeFace every time the View loads.
来源:https://stackoverflow.com/questions/23135238/how-to-set-same-font-family-to-entire-android-application