0
什么是最简单和最快的方式来改变字体android项目,我想避免改变每一个视图。为所有的android项目添加默认的ttf字体
什么是最简单和最快的方式来改变字体android项目,我想避免改变每一个视图。为所有的android项目添加默认的ttf字体
您可以为所有视图制作自定义视图。
例如,为TextView的:通过使用这个天秤座
<PackageName.MyTextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="Hello" />
:
public class MyTextView extends TextView {
private Typeface typeface;
public MyTextView(Context context) {
super(context);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.app);
String customFont = a.getString(R.styleable.app_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
private boolean setCustomFont(Context ctx, String asset) {
try {
if (typeface == null) {
typeface = Typeface.createFromAsset(ctx.getAssets(),
getResources().getString(R.string.app_font1));
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
setTypeface(typeface);
return true;
}
}
在String.xml
添加此标签 -
<string name="app_font1">fonts/Roboto-Regular.ttf</string>
在XML文件RY你不需要改变每一个视图,只需创建一个自定义Activity
类,并重写此方法:
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
,然后扩展所有的活动与这一活动。
可能重复[是否可以为整个应用程序设置字体?](http://stackoverflow.com/questions/2711858/is-it-possible-to-set-font-for-entire-application) – Fllo
以上链接的可能重复 –