问题
How can I change the entire typeface of my android application?
previously I saw this post on github.The solution work fine only for devices with lower than api 21.
For android 5 this method doesn't work even if we add a values-v21
folder with styles.xml
separately.
This is my values-v21/styles.xml
:
<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
</style>
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:typeface">serif</item>
</style>
</resources>
回答1:
<style name="AppTheme" parent="AppBaseTheme">
<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.Holo.Button">
<item name="android:fontFamily">sans-serif-light</item>
</style>
Then Apply theme to your app
<application
android:theme="@style/AppTheme" >
</application>
回答2:
One suggestion from is to create Custom Textview that extends android TextView.I am including sample code below
public class CustomTextView extends TextView{
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
// TODO Auto-generated constructor stub
}
public CustomTextView(Context context) {
super(context);
init();
// TODO Auto-generated constructor stub
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
// TODO Auto-generated constructor stub
}
private void init(){
Typeface font_type=Typeface.createFromAsset(getContext().getAssets(), "font/ProximaNova_Reg.ttf");
setTypeface(font_type);
}
}
And put your font in android asset/font folder.You can do it for Button,EditText etc.
来源:https://stackoverflow.com/questions/31218795/changing-all-typefaces-of-an-android-application