本文介绍了从资产访问一次字样,并用它作为参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个返回titleFont和contentFont字样,这样会提高效率只是回归基准,减少code长度的方法。我知道它很容易,但我没有得到的方式。任何人都可以帮助请。或任何替代品将AP preciated ..对不起,我英文不好

下面是将要运行几十倍的方法。

 保护无效onPostExecute(条结果){
    super.onPostExecute(结果);

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

    私人字样titleFont = Typeface.createFromAsset(context.getAssets(),字体/ InterstateCondMonoLgt.ttf);
    私人字样contentFont = Typeface.createFromAsset(context.getAssets(),字体/ InterstateLight.ttf);

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

解决方案

我希望这帮助你;)

FontUtil类

 公共类FontUtil {

    私有静态字样mTitleFont;
    私有静态字样mContentFont;

    公共静态枚举FontType {

        TITLE_FONT {
            公共字符串的toString(){
                回字型/ InterstateCondMonoLgt.ttf;
            }
        },

        CONTENT_FONT {
            公共字符串的toString(){
                回字型/ InterstateLight.ttf;
            }
        }
    }

    / **
     返回:字样实例与作为参数传递的字体
     * /
    公共静态字样getTypeface(上下文的背景下,字符串typefaceName){
        字样字体= NULL;

        尝试 {

            如果(typefaceName.equals(FontType.TITLE_FONT.toString())){

                如果(mTitleFont == NULL){
                    mTitleFont = Typeface.createFromAsset(
                            context.getAssets(),字体/+ typefaceName);
                }

                字体= mTitleFont;

            }否则,如果(typefaceName.equals(FontType.CONTENT_FONT.toString())){

                如果(mContentFont == NULL){
                    mContentFont = Typeface.createFromAsset(
                            context.getAssets(),字体/+ typefaceName);
                }

                字体= mContentFont;

            }

        }赶上(例外前){
            字体= Typeface.DEFAULT;
        }

        返回字样;
    }

    / **
     返回:字样实例与作为参数传递的字体
     * /
    公共静态字样getTypeface(上下文的背景下,FontType typefaceName){
        返回getTypeface(上下文,typefaceName.toString());
    }
}
 

客户端

 保护无效onPostExecute(条结果){
    super.onPostExecute(结果);

    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());
}
 

I am trying to create a method that returns titleFont and contentFont typeface so that it would improve efficiency by just returning reference and reduce length of code. I know its easy but I am not getting the way. Can anyone help please. or any alternatives would be appreciated.. Sorry for bad english

Here is the method that will be running several times..

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());
}
解决方案

I hope this help you ;)

FontUtil class

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(), "fonts/" + typefaceName);
                }

                typeFace = mTitleFont;

            } else if (typefaceName.equals(FontType.CONTENT_FONT.toString())) {

                if (mContentFont == null) {
                    mContentFont = Typeface.createFromAsset(
                            context.getAssets(), "fonts/" + 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());
    }
}

Client

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());
}

这篇关于从资产访问一次字样,并用它作为参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 22:39