我正在做一个安卓项目,我想改变所有应用程序的默认字体,而不仅仅是一个TextView或按钮。
我的自定义字体存储为assets/fonts/font.ttf
发布于 2015-06-03 16:14:26
使用书法图书馆!它完全是针对这个问题而设计的。
https://github.com/chrisjenx/Calligraphy
从文件中:
在扩展类中,Application
将这段代码放入其中。
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/font.ttf")
.setFontAttrId(R.attr.fontPath)
.build()
);
然后在每个Activity
中
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
发布于 2015-06-03 15:37:34
您所要做的就是:首先创建像这个这样的字体对象:
Typeface typeFace= Typeface.createFromAsset(getAssets(),
"fonts/font.ttf");
并在任何需要的地方设置此字体,如:
textView.setTypeFace(typeFace);
你不能仅仅通过把你的字体文件放在资产中来改变整个应用程序的字体。
发布于 2015-06-03 16:03:23
我以前就得处理这件事。这很烦人,因为没有什么好办法。我发现它的最佳方式是覆盖文本视图,而不是使用自定义类。上这门课
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* Created by shemesht on 6/3/15.
*/
public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context);
Typeface typeFace= Typeface.createFromAsset(getContext().getAssets(), "fonts/font.ttf");
setTypeface(typeFace);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
Typeface typeFace= Typeface.createFromAsset(getContext().getAssets(), "fonts/font.ttf");
setTypeface(typeFace);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
Typeface typeFace= Typeface.createFromAsset(getContext().getAssets(), "fonts/font.ttf");
setTypeface(typeFace);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
Typeface typeFace= Typeface.createFromAsset(getContext().getAssets(), "fonts/font.ttf");
setTypeface(typeFace);
}
}
然后在xml中
<com.yourApp.Namehere.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this text has a new font!"/>
就是这样。您仍然可以在java中将文本视图作为常规引用。
https://stackoverflow.com/questions/30624566
复制相似问题