为完整的Android应用程序添加自定义字体

Chr*_*han 32 android textview custom-font

在我的应用程序中,我需要对所有textview和编辑文本字段使用helvetica字体.除了对每个textview使用settypeface方法之外,还有什么方法可以做到这一点吗?任何建议都会有很大的帮助.

提前致谢 !

Chr*_*han 64

我自己想通了.这是我使用的代码.我创建自TextView定义字体作为默认字体的自定义.

public class MyTextView extends TextView {

    public MyTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyTextView(Context context) {
        super(context);
        init();
    }

    private void init() {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/chiller.ttf");
        setTypeface(tf ,1);

    }

}
Run Code Online (Sandbox Code Playgroud)

  • @mxg通过覆盖TextView,他可以在他的布局文件中使用MyTextView,而无需在activity/fragment中以编程方式执行任何操作来实现自定义字体. (9认同)
  • @mxg我没听懂你说的话 (2认同)

lit*_*ium 27

在你的活动中,在你打电话之后

的setContentView(R.id.blahblah);

你应该运行一个方法来遍历整个小部件层次结构并处理字体替换,比如;

setContentView(R.id.blahblah);
Utils.overrideFonts(this, findViewById(android.R.id.content));
Run Code Online (Sandbox Code Playgroud)

并且提到的"overrideFonts"方法应该是这样的;

public static void overrideFonts(final Context context, final View v) {
    try {
        if (v instanceof ViewGroup) {
            ViewGroup vg = (ViewGroup) v;
            for (int i = 0; i < vg.getChildCount(); i++) {
                View child = vg.getChildAt(i);
                overrideFonts(context, child);
            }
        } else if (v instanceof TextView) {
            ((TextView)v).setTypeface(FONT_REGULAR);
        }
    } catch (Exception e) {
        e.printStackTrace();
        // ignore
    }
}
Run Code Online (Sandbox Code Playgroud)

在这个方案中,FONT_REGULAR应该在某个地方安全地初始化,你可能想要一个单例或其他方式来确保它被正确初始化...

private static void initializeFonts(final Context context) {
    FONT_REGULAR = Typeface.createFromAsset(context.getAssets(), "fonts/myfont_medium.otf");
    FONT_BOLD = Typeface.createFromAsset(context.getAssets(), "fonts/myfont_bold.otf");
}
Run Code Online (Sandbox Code Playgroud)

如果您使用Activity的子类,如MyAppActivity(extends Activity),那么您不需要更改每个Activity类以进行此类自定义.相反,你可以切入它并覆盖这样的行为;

public class MyAppActivity extends Activity {
... ...
    @Override
    public void setContentView(final int layoutResID) {
        super.setContentView(layoutResID);
        Utils.overrideFonts(this, findViewById(android.R.id.content));
    }
... ...
}
Run Code Online (Sandbox Code Playgroud)

通过这种方式,您可以使用您的任何活动来获得共同的行为;

public class SettingsUI extends MyAppActivity {
... ...
} 
Run Code Online (Sandbox Code Playgroud)

我希望它有所帮助......干杯!

  • 对于简单的布局,这是一个很好的解决方案.但是,如果您的活动中有ListView,则无效.ListView中的项目在setContent方法之后实例化,因此列表中的TextView不会受此方法的影响 (6认同)
  • 动作栏的字体怎么样? (2认同)

Han*_*nry 9

创建一个样式并使用它所有的文本属性.

<style name="CustomText">
    <item name="android:typeface">YourFontName</item>
</style>
Run Code Online (Sandbox Code Playgroud)

用它:

<TextView style="@style/CustomText" />
Run Code Online (Sandbox Code Playgroud)

以上是在所有活动中使用自定义字体进行自定义,您可以使用....

Typeface font = Typeface.createFromAsset(getAssets(), "CustomFontName.ttf");  
txt.setTypeface(font);
Run Code Online (Sandbox Code Playgroud)

试试这个.

  • android:字体字段只允许使用内置字体名称.如何指定资产文件夹中的自定义字体名称? (11认同)
  • @hanry你不能在android:typeface字段中使用自定义字体名称.你的答案的第二部分将起作用,但样式部分不会. (3认同)
  • 这不回答这个问题."除了对每个textview使用settypeface方法之外,还有什么方法可以做到这一点吗?" (2认同)

Nik*_*huk 6

要在所有应用程序中应用自定义字体,只需创建以下活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FontManager.getInstance().initialize(this, R.xml.fonts);
    setContentView(R.layout.main);
}
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
    SimpleFactory factory = new SimpleFactory();
    return factory.onCreateView(name, context, attrs);
}
Run Code Online (Sandbox Code Playgroud)

其中FontManager是管理在/xml/fonts.xml和SimpleFactory中定义的所有字体的类,用于创建视图并将自定义字体应用于每个视图的文本视图实例.

/xml/fonts.xml

<?xml version="1.0" encoding="utf-8"?>
<familyset>
    <family>
        <nameset>
            <!--Font name-->
            <name>HelveticaNeueLTStd</name>
        </nameset>
        <fileset>
            <!--Font styles-->
            <file style="normal">fonts/HelveticaNeueLTStd-LtCn.otf</file>
            <file style="bold">fonts/HelveticaNeueLTStd-MdCn.otf</file>
            <file style="italic">fonts/HelveticaNeueLTStd-LtCnO.otf</file>
            <file style="bold_italic">fonts/HelveticaNeueLTStd-MdCnO.otf</file>
        </fileset>
    </family>
    <family>
        <!--There new font family can be added,
don't forget add font files into /assets/fonts directory and
put the name of the font into /values/string/font.xml-->
    </family>
</familyset>
Run Code Online (Sandbox Code Playgroud)

FontFactory - abastract类,只是扩展它以创建自己的工厂

public abstract class FontFactory implements LayoutInflater.Factory{
    public final String TAG = getClass().getSimpleName();

    static final Class<?>[] mConstructorSignature = new Class[] {Context.class, AttributeSet.class};
    final Object[] mConstructorArgs = new Object[2];
    private static final String[] sClassPrefixList = {
            "android.widget.",
            "android.webkit."
    };

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        if("ViewStub".equals(name) || "View".equals(name)){
            return null;
        }
        View view = null;
        Constructor<? extends View> constructor = null;
        Class clazz = null;

        if (view == null) {
            if (-1 == name.indexOf('.')) {
                for (String prefix : sClassPrefixList) {
                    clazz = getClazz(prefix, name);
                    if(clazz != null){
                        break;
                    }
                }
            } else {
                clazz = getClazz("", name);
            }
        }

        if (clazz == null) {
            Log.d(TAG, "View can't be created " + name);
            return null;
        }

        try {
            constructor = clazz.getConstructor(mConstructorSignature);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

        Object[] args = mConstructorArgs;
        args[1] = attrs;

        if(constructor == null){
            return null;
        }

        try {
                view = constructor.newInstance(context, attrs);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        if(view != null){
            onFontApply(context, view);
        }
        return view;
    }

    public abstract void onFontApply(Context context, View view);

    private Class getClazz(String prefix, String name){
        Class clazz = null;
        try {
            clazz = Class.forName(prefix + name);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            return clazz;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

FontManager - 只需将/xml/fonts.xml中定义的字体文件映射到/ assets目录中的字体文件,并按字体系列名称和字体样式返回TypeFace.

public void initialize(Context context, int resId) {
    if(mFonts != null){
        Log.d(TAG,"FontManager have already initialized");
        return;
    }
    XmlResourceParser parser = null;
    try {
        parser = context.getResources().getXml(resId);
        mFonts = new ArrayList<Font>();

        String tag;
        String fontStryleAttr = null;
        int eventType = parser.getEventType();

        Font font = null;

        do {
            tag = parser.getName();

            switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (tag.equals(TAG_FAMILY)) {
                        // one of the font-families.
                        font = new Font();
                    } else if (tag.equals(TAG_NAMESET)) {
                        // a list of font-family names supported.
                        font.families = new ArrayList<String>();
                    } else if (tag.equals(TAG_NAME)) {
                        isName = true;
                    } else if (tag.equals(TAG_FILESET)) {
                        // a list of files specifying the different styles.
                        font.styles = new ArrayList<FontStyle>();
                    } else if (tag.equals(TAG_FILE)) {
                        isFile = true;
                        fontStryleAttr = parser.getAttributeValue(null, ATTR_STYLE);
                    }
                    break;

                case XmlPullParser.END_TAG:
                    if (tag.equals(TAG_FAMILY)) {
                        // add it to the list.
                        if (font != null) {
                            mFonts.add(font);
                            font = null;
                        }
                    } else if (tag.equals(TAG_NAME)) {
                        isName = false;
                    } else if (tag.equals(TAG_FILE)) {
                        isFile = false;
                        fontStryleAttr = null;
                    }
                    break;

                case XmlPullParser.TEXT:
                    String text = parser.getText();
                    if (isName) {
                        // value is a name, add it to list of family-names.
                        if (font.families != null)
                            font.families.add(text);
                    } else if (isFile) {
                        // value is a file, add it to the proper kind.
                        FontStyle fontStyle = new FontStyle();
                        fontStyle.font = Typeface.createFromAsset(context.getAssets(), text);
                        String attr = parser.getAttributeValue(null, ATTR_STYLE);
                        if (fontStryleAttr.equals(STYLE_BOLD))
                            fontStyle.style = Typeface.BOLD;
                        else if (fontStryleAttr.equals(STYLE_ITALIC))
                            fontStyle.style = Typeface.ITALIC;
                        else if (fontStryleAttr.equals(STYLE_BOLD_ITALIC))
                            fontStyle.style = Typeface.BOLD_ITALIC;
                        else
                            fontStyle.style = Typeface.NORMAL;
                        font.styles.add(fontStyle);
                    }
            }

            eventType = parser.next();

        } while (eventType != XmlPullParser.END_DOCUMENT);

    } catch (XmlPullParserException e) {
        throw new InflateException("Error inflating font XML", e);
    } catch (IOException e) {
        throw new InflateException("Error inflating font XML", e);
    } finally {
        if (parser != null)
            parser.close();
    }
}

public Typeface get(String family, int style) {
    for (Font font: mFonts) {
        for (String familyName : font.families) {
            if (familyName.equals(family)) {
                // if no style in specified, return normal style.
                if (style == -1)
                    style = Typeface.NORMAL;
                for (FontStyle fontStyle : font.styles) {
                    if (fontStyle.style == style)
                        return fontStyle.font;
                }
            }
        }
    }
    return mDefaultFont;
}
Run Code Online (Sandbox Code Playgroud)

有关更多代码和示例,请查看此处