我应该把我的字体文件(TTF)放在res文件夹的什么地方?
发布于 2012-06-07 03:12:33
您可以在asset文件夹中创建字体(即asset/fonts/roboto.ttf)。
然后,为您的TextView创建一个适当的类:
// RobotoFont class
package com.my.font;
public class RobotoFont extends TextView {
public RobotoFont(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public RobotoFont(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RobotoFont(Context context) {
super(context);
}
public void setTypeface(Typeface tf, int style) {
if (style == Typeface.BOLD) {
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Bold.ttf"));
}
else if(style == Typeface.ITALIC)
{
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Italic.ttf"));
}
else
{
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Regular.ttf"));
}
}
}
最后,更新你的布局:
//main.xml
//replace textview with package name com.my.font.RobotoFont
<com.my.font.RobotoFont
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="2dip" />
https://stackoverflow.com/questions/10920438
复制相似问题