Android-如何为整个应用程序设置自定义字体

Android-如何为整个应用程序设置自定义字体,android,text,fonts,Android,Text,Fonts,可能重复: 我需要为整个应用程序设置自定义字体(.ttf格式),我怎么做? 如果可能的话,从清单或XML将是最好的选择 TextView tv=(TextView)findViewById(R.id.custom); Typeface face=Typeface.createFromAsset(getAssets(), "fonts/Verdana.ttf"); tv.setTypeface(

可能重复:

我需要为整个应用程序设置自定义字体(.ttf格式),我怎么做? 如果可能的话,从清单或XML将是最好的选择

TextView tv=(TextView)findViewById(R.id.custom);
    Typeface face=Typeface.createFromAsset(getAssets(),
                                          "fonts/Verdana.ttf");

    tv.setTypeface(face);
将字体文件放入/res/font文件夹中


喜欢我的答案,如果有帮助的话…

编辑2014年9月:

对于仍然看到这个可怕答案的人来说,真正好的答案就在这里:


旧版:

你最好的选择是:

所以


将.ttf放在“资产”文件夹的根目录中。

创建一个TextView子类,并在任何地方使用它

    public class RobotoTextView extends TextView {

    public RobotoTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public RobotoTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RobotoTextView(Context context) {
        super(context);
        init();
    }

    private void init() {
        if (!isInEditMode()) {
            Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf");
            setTypeface(tf);
        }
    }

}
public class MyTextView extends android.widget.TextView
{

    public MyTextView(Context context)
    {
        this(context, null);
    }

    public MyTextView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public MyTextView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs);
        if (this.isInEditMode()) return ;

        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SomeStyle);
        final String customFont = a.getString(R.styleable.SomeStyle_font);

        //Build a custom typeface-cache here!
        this.setTypeface(
            Typeface.createFromAsset(context.getAssets(), customFont)
        );  
    }
}
用法



在java代码中,您可以将其强制转换为TextView。您可以根据建议扩展TextView并设置字体,然后在整个应用程序中使用它。

您可以这样做。 创建一个自定义文本视图,并在任何地方使用该视图

    public class RobotoTextView extends TextView {

    public RobotoTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public RobotoTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RobotoTextView(Context context) {
        super(context);
        init();
    }

    private void init() {
        if (!isInEditMode()) {
            Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf");
            setTypeface(tf);
        }
    }

}
public class MyTextView extends android.widget.TextView
{

    public MyTextView(Context context)
    {
        this(context, null);
    }

    public MyTextView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public MyTextView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs);
        if (this.isInEditMode()) return ;

        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SomeStyle);
        final String customFont = a.getString(R.styleable.SomeStyle_font);

        //Build a custom typeface-cache here!
        this.setTypeface(
            Typeface.createFromAsset(context.getAssets(), customFont)
        );  
    }
}
将此添加到attrs.xml

<declare-styleable name="SomeStyle">
    <attr name="font" format="string" />
</declare-styleable>


但我也使用android样式自动在应用程序中的所有文本视图上使用它。

这很好,但是如果我有一个ListView,我如何更改行的字体?我想可以通过xml来实现这个解决方案在安卓5.0上似乎不再有效了。还有其他选择吗?@AndreaMarioLufino将适配器中的这种字体设置为所有文本视图都可以。假设您有
textview1、textview2
textView3
,而不应该在它们上使用setTypface(您的字体)。希望有帮助。嗨!它在这一行显示错误
@style/MyTextView
**我现在要做的事需要一个'type'属性?我通过添加
type=“string”
字体未缓存来清除错误,因此如果您计划使用此代码,您还应该构建自定义字体缓存如何构建缓存?您可以在某个地方有一个静态单例类,其中包含对加载字体的引用的hashmap。请确保在应用程序关闭时将其清除!MyPathToFontinaSets.ttf如何在此处设置路径?有什么例子吗?谢谢~仅仅因为它被标记为重复,你就无法提供更好的答案:使用书法库来实现这一点。它甚至改变了祝酒词的字体。你不就用它吗
<item name="android:textViewStyle">@style/MyTextView</item>
<style name="MyTextView" parent="@android:style/Widget.TextView">
    <item name="font">MyPathToFontInAssets.ttf</item>
</style>