2

我正在尝试创建一个返回 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());
}
4

4 回答 4

6

我希望这对你有帮助;)

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());
}
于 2013-09-17T10:47:08.357 回答
1

您可以在项目中创建一个实用程序类

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);在需要的任何地方调用字体

于 2013-09-17T10:41:01.463 回答
0

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

于 2013-09-17T10:40:32.917 回答
0

创建一些 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());
于 2013-09-17T10:42:40.320 回答