安卓开发如何自定义字体

   下面有段代码关于android 自定义字体 希望对安卓软件的开发人员有一定的帮助!
 
    android:自定义字体;
    static public void setFont(Context context, ViewGroup containerLayout) {
    Typeface typeface = Typeface.createFromAsset(context.getAssets(),
            "fonts/Comfortaa-Regular.ttf");

    View view;
    ViewGroup viewGroup;
    TextView textView;
    Button button;
    int count = containerLayout.getChildCount();
    for (int i = 0; i < count; i++) {
        view = containerLayout.getChildAt(i);

        if (view instanceof ViewGroup) {
            viewGroup = (ViewGroup) view;
            setFont(context, viewGroup);
        } else if (view instanceof TextView) {
            textView = (TextView) view;
            textView.setTypeface(typeface);
        }
    }
}

// 调用上面的代码。
ViewGroup containerLayout = (ViewGroup) findViewById(R.id.containerLayout);
TypeFaceHelpers.setFont(this.getApplicationContext(), containerLayout);

posted on 2012-04-25 17:10  迷途羊  阅读(384)  评论(0编辑  收藏  举报

导航