Android 上的自定义字体和自定义文本视图 [英] Custom Fonts and Custom Textview on Android
问题描述
从我需要开发的应用程序中,我收到了包含许多文件的特定字体,例如 FontName-Regular、FontName-Bold、FontName-it.我需要在应用程序的所有文本视图中使用它.首先,我认为这是一项简单的任务.查看 SO,发现一个非常好的线程:这里
From an application I need to develop, I've received a specific font that has many files like FontName-Regular, FontName-Bold, FontName-it. I need to use it in all the textviews in the application. First I thought it was an easy task. Look over SO and found a very nice thread:here
所以首先我确实喜欢:
public static void overrideFonts(final Context context, final View v) {
try {
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
overrideFonts(context, child);
}
} else if (v instanceof TextView) {
((TextView)v).setTypeface(FONT_REGULAR);
}
} catch (Exception e) {
e.printStackTrace();
// ignore
}
}
并在我的活动中的 onCreate 期间调用了此方法.我的应用程序中的每个 textView 都显示了那个字体和男孩,我很高兴能如此轻松地离开.直到我进入一个屏幕,其中一些文本视图需要 Bold 作为 Style
(android:textStyle="bold"
).然后我意识到这个解决方案不能让我从资产中加载 Font-Bold.ttf.
And called this method during onCreate in my activity. Every textView in my app was showing that font and boy, was I happy for getting away so easy. Until I got to a screen where some textviews required Bold as Style
(android:textStyle="bold"
). Then I realized that this solution does not provide me with possibility to load the Font-Bold.ttf from assets.
在同一个 SO 问题中,进一步查看并看到了一个不错的自定义 TextView 实现:
Than looked further and saw a nice custom TextView implementation, in the same SO question:
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context) {
super(context);
init();
}
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/chiller.ttf");
setTypeface(tf ,1);
}
}
这看起来更好.我的问题是:如何在 init()
上检测我的控件是否将 Style 设置为 Bold 以便我可以分配请求的 TypeFace ?
This looks even better. My question is: how can I detect on init()
if my control has Style set to Bold or not so I can assign the requested TypeFace ?
感谢您的宝贵时间.
乐.按照下面的示例,我将我的课程更新为:
LE. Following the example below, I've updated my class as:
public class MyTextView extends TextView {
Typeface normalTypeface = Typeface.createFromAsset(getContext().getAssets(), Constants.FONT_REGULAR);
Typeface boldTypeface = Typeface.createFromAsset(getContext().getAssets(), Constants.FONT_BOLD);
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyTextView(Context context) {
super(context);
}
public void setTypeface(Typeface tf, int style) {
if (style == Typeface.BOLD) {
super.setTypeface(boldTypeface/*, -1*/);
} else {
super.setTypeface(normalTypeface/*, -1*/);
}
}
}
好吧,如果我调试,应用程序进入 setTypeFace 并且它似乎应用了粗体,但在我的布局上我看不到任何变化,而不是粗体.无论我使用什么字体,我的 TextView 都不会进行任何更改,并使用默认的 android 字体显示.我想知道为什么?
Well If I debug, the app goes in setTypeFace and it seems to apply the bold one, but on my layout I can't see any change, not bold. No matter what font I use, no changes are done in my TextView and is displayed with the default android font. I wonder why ?
我在一篇博文中总结了所有内容 在我的博客上 也许它会对某人有所帮助.
I have summed everything on a blog post here on my blog maybe it will help someone.
推荐答案
TextView
的构造函数调用setTypeface(Typeface tf, int style)
,style从 XML 属性
参数.因此,如果您想拦截此调用以强制使用您自己的字体,您可以重写此方法,如下所示:android:textStyle
检索的
The constructor of TextView
calls setTypeface(Typeface tf, int style)
with the style
parameter retrieved from the XML attribute android:textStyle
. So, if you want to intercept this call to force your own typeface you can override this method as follow:
public void setTypeface(Typeface tf, int style) {
Typeface normalTypeface = Typeface.createFromAsset(getContext().getAssets(), "font/your_normal_font.ttf");
Typeface boldTypeface = Typeface.createFromAsset(getContext().getAssets(), "font/your_bold_font.ttf");
if (style == Typeface.BOLD) {
super.setTypeface(boldTypeface/*, -1*/);
} else {
super.setTypeface(normalTypeface/*, -1*/);
}
}
这篇关于Android 上的自定义字体和自定义文本视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!