安卓:要设置自定义字体为整个应用程序不是运行 [英] Android: Want to set custom fonts for whole application not runtime

查看:153
本文介绍了安卓:要设置自定义字体为整个应用程序不是运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以设置任何自定义的字体应用程序中的每一个控制?并不见得运行时? (即从XML如果可能的话,或只有一次在JAVA文件整个应用程序)

Is it possible to set any custom font in every control of the application? And not necessarily runtime ? (i.e. from xml if possible or only once for whole application in JAVA file)

我可以设置字体从这个code一个控制。

I can set the font for one control from this code.

public static void setFont(TextView textView) {
    Typeface tf = Typeface.createFromAsset(textView.getContext()
            .getAssets(), "fonts/BPreplay.otf");

    textView.setTypeface(tf);

}

而与此code中的问题是,它应该被称为为每个控制。我想打电话给这个或任何类似的方法一次,或者如果可能的话设置XML属性。这可能吗?

And the problem with this code is it should be called for every control. And i want to call this or any similar method once, or if possible set the property in xml. Is it possible?

推荐答案

修改:所以这已经有一段时间了,我想补充我认为是这样做的最好办法,并通过XML不能少!

EDIT: So it's been a while, and I'd like to add what I think is the best way to do this, and through XML no less!

因此​​,首先,你要想让重写任何视图要自定义一个新的类。 (如想使用自定义字体一个按钮?延长按钮)。让我们一个例子:

So first, you're going to want to make a new class that overrides whatever View you want to customize. (e.g. want a Button with a custom typeface? Extend Button). Let's make an example:

public class CustomButton extends Button {
    private final static int ROBOTO = 0;
    private final static int ROBOTO_CONDENSED = 1;

    public CustomButton(Context context) {
        super(context);
    }

    public CustomButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        parseAttributes(context, attrs); //I'll explain this method later
    }

    public CustomButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        parseAttributes(context, attrs);
    }
}

现在,如果你没有一个,添加一个XML文档在 RES /价值/ attrs.xml ,并添加:

Now, if you don't have one, add an XML document under res/values/attrs.xml, and add:

<resources>
    <!-- Define the values for the attribute -->
    <attr name="typeface" format="enum">
        <enum name="roboto" value="0"/>
        <enum name="robotoCondensed" value="1"/>
    </attr>

    <!-- Tell Android that the class "CustomButton" can be styled, 
         and which attributes it supports -->
    <declare-styleable name="CustomButton">
        <attr name="typeface"/>
    </declare-styleable>
</resources>

好了,有了这样的方式,让我们回到 parseAttributes()方法,从早期的:

private void parseAttributes(Context context, AttributeSet attrs) {
    TypedArray values = context.obtainStyledAttributes(attrs, R.styleable.CustomButton);

    //The value 0 is a default, but shouldn't ever be used since the attr is an enum
    int typeface = values.getInt(R.styleable.CustomButton_typeface, 0);

    switch(typeface) {
        case ROBOTO: default:
            //You can instantiate your typeface anywhere, I would suggest as a 
            //singleton somewhere to avoid unnecessary copies
            setTypeface(roboto); 
            break;
        case ROBOTO_CONDENSED:
            setTypeface(robotoCondensed);
            break;
    }

    values.recycle();
}

现在你所有的设置。您可以添加更多的属性,任何事情(你可以添加另一个用于typefaceStyle - 粗体,斜体等),但现在让我们来看看如何使用它:

Now you're all set. You can add more attributes for about anything (you could add another one for typefaceStyle -- bold, italic, etc.) but now let's see how to use it:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res/com.yourpackage.name"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.yourpackage.name.CustomButton
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me!"
        custom:typeface="roboto" />

</LinearLayout>

的xmlns:定制行确实可以是任意的,但该公约是上面显示真实。重要的是,它是独一无二的,这就是为什么包名。现在,你只需要使用自定义的: preFIX你的属性,而机器人: preFIX为Android的属性。

The xmlns:custom line can really be anything, but the convention is what's shown above. What matters is that it is unique, and that's why the package name is used. Now you just use the custom: prefix for your attributes, and the android: prefix for android attributes.

最后一件事:如果你想使用这一种风格( RES /价值/ styles.xml ),你应该的没有的补充在的xmlns:定制行。刚才提到没有preFIX属性的名称:

One last thing: if you want to use this in a style (res/values/styles.xml), you should not add the xmlns:custom line. Just reference the name of the attribute with no prefix:

<style name="MyStyle>
    <item name="typeface">roboto</item>
</style>


                               (PREVIOUS ANSWER)

使用Android中自定义字体

这应该有所帮助。基本上,有没有办法做到这一点的XML,而据我所知,没有更简单的方法来做到这一点在code。你总是可以有一个setLayoutFont()方法创建的字样一次,然后运行setTypeface()为每个。你只需要每次一个新的项目添加到布局时间来更新它。喜欢的东西如下:

This should help. Basically, there's no way to do this in XML, and as far as I can tell, no easier way to do it in code. You could always have a setLayoutFont() method that creates the typeface once, then runs setTypeface() for each. You'd just have to update it each time you add a new item to a layout. Something like below:

public void setLayoutFont() {
    Typeface tf = Typeface.createFromAsset(
        getBaseContext().getAssets(), "fonts/BPreplay.otf");
    TextView tv1 = (TextView)findViewById(R.id.tv1);
    tv1.setTypeface(tf);

    TextView tv2 = (TextView)findViewById(R.id.tv2);
    tv2.setTypeface(tf);

    TextView tv3 = (TextView)findViewById(R.id.tv3);
    tv3.setTypeface(tf);
}

修改:所以我就抽时间去实现这样的事情我自己,我怎么落得这样做这是使一个函数像这样的:

EDIT: So I just got around to implementing something like this myself, and how I ended up doing it was making a function such as this:

public static void setLayoutFont(Typeface tf, TextView...params) {
    for (TextView tv : params) {
        tv.setTypeface(tf);
    }
}

然后,只需使用来自的onCreate()这个方法,并通过所有的TextViews要更新:

Then, just use this method from onCreate(), and pass all the TextViews you want to update:

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/BPreplay.otf");
//find views by id...
setLayoutFont(tf, tv1, tv2, tv3, tv4, tv5);

修改12年9月5日:

所以,因为这仍然是获​​得意见和表决,我想补充一个更好的,更全面的方法:

So since this is still getting views and votes, I'd like to add a much better and more complete method:

Typeface mFont = Typeface.createFromAsset(getAssets(), "fonts/BPreplay.otf");
ViewGroup root = (ViewGroup)findViewById(R.id.myrootlayout);
setFont(root, mFont);

/*
 * Sets the font on all TextViews in the ViewGroup. Searches
 * recursively for all inner ViewGroups as well. Just add a
 * check for any other views you want to set as well (EditText,
 * etc.)
 */
public void setFont(ViewGroup group, Typeface font) {
    int count = group.getChildCount();
    View v;
    for(int i = 0; i < count; i++) {
        v = group.getChildAt(i);
        if(v instanceof TextView || v instanceof Button /*etc.*/)
            ((TextView)v).setTypeface(font);
        else if(v instanceof ViewGroup)
            setFont((ViewGroup)v, font);
    }
}

如果您通过它的布局的根,它会递归地检查的TextView 按钮的意见(或其他任何你添加到该if语句)的布局中,并设置字体,您无需按ID指定。当然,这是假设你要设置字体的每次的看法。

If you pass it the root of your layout, it will recursively check for TextView or Button views (or any others you add to that if statement) within that layout, and set the font without you having to specify them by ID. This of course is assuming you want to set the font to every view.

这篇关于安卓:要设置自定义字体为整个应用程序不是运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆