Android 从资源中访问一次字体并将其用作参考

Android 从资源中访问一次字体并将其用作参考,android,Android,我正在尝试创建一个返回titleFont和contentFont字体的方法,以便通过只返回引用和减少代码长度来提高效率。我知道这很容易,但我不懂。谁能帮忙吗。或者任何其他选择都将不胜感激。。对不起,英语不好 下面是将运行多次的方法 protected void onPostExecute(Article result) { super.onPostExecute(result); TextView txtTitle= (TextView) view.findViewById(R

我正在尝试创建一个返回titleFont和contentFont字体的方法,以便通过只返回引用和减少代码长度来提高效率。我知道这很容易,但我不懂。谁能帮忙吗。或者任何其他选择都将不胜感激。。对不起,英语不好

下面是将运行多次的方法

protected void onPostExecute(Article result) {
    super.onPostExecute(result);

    TextView txtTitle= (TextView) view.findViewById(R.id.title);
    txtTitle.setTypeface(titleFont);
    txtTitle.setText(result.getTitle());

    private Typeface titleFont=Typeface.createFromAsset(context.getAssets(),"fonts/InterstateCondMonoLgt.ttf");
    private Typeface contentFont=Typeface.createFromAsset(context.getAssets(),"fonts/InterstateLight.ttf");

    TextView txtMain= (TextView) view.findViewById(R.id.main);
    txtMain.setTypeface(contentFont);
    txtMain.setText(result.getContent());
}

在特定方法中创建的所有变量都是私有的不能为方法中声明的任何变量提供访问修饰符。它会给你带来编译错误。
我建议您将这个typefaces变量声明为类级变量,并在ASYNCTASK的构造函数中初始化它们。否则,每次调用onPostExecute()时,每次都会创建一个字体,这可能会导致以后的内存开销。

您可以在项目中使用

private Typeface titleFont=Typeface.createFromAsset(context.getAssets(),"fonts/InterstateCondMonoLgt.ttf");
private Typeface contentFont=Typeface.createFromAsset(context.getAssets(),"fonts/InterstateLight.ttf");
然后简单地为它编写geter,就像

public Typeface getTitleFont() {
    return titleFont;
}
或者我更喜欢的是,如果您继承了放在基类中的类,比如:

public static Typeface titleFont;
然后

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    titleFont=Typeface.createFromAsset(context.getAssets(),"fonts/InterstateCondMonoLgt.ttf");
}
这样,您将始终拥有字体
只需调用
textView.setTypeface(titleFont)任何需要的地方

创建一些util类并将所有代码放在那里,如:

public class Utils
{
    public static Typeface getTitleFont()
    {
        return Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/InterstateCondMonoLgt.ttf");
    }
}
然后像这样使用:

TextView txtMain= (TextView) view.findViewById(R.id.main);
txtMain.setTypeface(your_package.Utils.getTitleFont());
我希望这对你有帮助;)

FontUtil类

public class FontUtil {

    private static Typeface mTitleFont;
    private static Typeface mContentFont;

    public static enum FontType {

        TITLE_FONT {
            public String toString() {
                return "fonts/InterstateCondMonoLgt.ttf";
            }
        },

        CONTENT_FONT {
            public String toString() {
                return "fonts/InterstateLight.ttf";
            }
        }
    }

    /**
     * @return Typeface Instance with the font passed as parameter
     */
    public static Typeface getTypeface(Context context, String typefaceName) {
        Typeface typeFace = null;

        try {
            if (typefaceName.equals(FontType.TITLE_FONT.toString())) {
                if (mTitleFont == null) {
                    mTitleFont = Typeface.createFromAsset(context.getAssets(), typefaceName);
                }
                typeFace = mTitleFont;
            } else if (typefaceName.equals(FontType.CONTENT_FONT.toString())) {
                if (mContentFont == null) {
                    mContentFont = Typeface.createFromAsset(context.getAssets(), typefaceName);
                }
                typeFace = mContentFont;
            } 
        } catch (Exception ex) {
            typeFace = Typeface.DEFAULT;
        }

        return typeFace;
    }

    /**
     * @return Typeface Instance with the font passed as parameter
     */
    public static Typeface getTypeface(Context context, FontType typefaceName) {
        return getTypeface(context, typefaceName.toString());
    }
}
客户端

protected void onPostExecute(Article result) {
    super.onPostExecute(result);

    TextView txtTitle= (TextView) view.findViewById(R.id.title);
    txtTitle.setTypeface( FontUtil.getTypeface(mContext, FontType.TITLE_FONT) );
    txtTitle.setText(result.getTitle());

    TextView txtMain= (TextView) view.findViewById(R.id.main);
    txtMain.setTypeface( FontUtil.getTypeface(mContext, FontType.CONTENT_FONT) );
    txtMain.setText(result.getContent());
}

看起来不错,但使用此方法时,每次调用该方法时都会创建字体。。。因此,这和我的解决方案是一样的。@Diago Palomar的解决方案只是创建了字体,只有当我厌倦了这个答案时,它抛出了一个例外:无法创建本机字体。需要做什么?我已经用了很多次了,它的工作就像一个魅力。但是,有一个Android bug报告可能是您出现问题的原因()。有关更多信息,请访问此线程:@DiegoPalomar,您的逻辑正常,但“font/”使用了两次。一个在enums下,另一个在“getTypeface”方法下。因为有两个,所以字体的路径类似于“assets/fonts/fonts/fontName.ttf”。它抛出本机字体异常。