自定义字体和自定义的TextView在Android
这是我需要开发一个应用程序,我已经收到了许多文件,如字体名正规特定的字体, FONTNAME粗体, FONTNAME-它的。我需要使用它在应用程序的所有textviews。首先,我认为这是一件容易的事。查看SO,发现了一个非常漂亮的主题:here
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的是显示出的字体和孩子,是我快乐的越来越远那么容易。直到我到了一个屏幕,在这里需要一些textviews 粗体作为风格
(安卓TEXTSTYLE =黑体
)。然后,我意识到,这个解决方案没有为我提供可能加载的字体粗体从资产.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.
看起来比进一步看到了一个漂亮的自定义TextView的实施,在相同的SO问题:
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()
如果我的控制已经样式设置为粗体或不那么我可以指定所需的字体
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。按照下面的例子中,我已经更新了我的类:
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 ?
我总结的一切在一篇博客文章here在我的博客也许这将帮助别人。
I have summed everything on a blog post here on my blog maybe it will help someone.
的的TextView
调用构造 setTypeface(字体TF,INT风格)
与风格从XML属性检索
参数安卓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*/);
}
}