为完整的安卓应用添加自定义字体

34

在我的应用程序中,我需要为所有的TextView和EditText字段使用Helvetica字体。除了对每个TextView使用setTypeface方法之外,还有其他方法吗?任何建议都将是一个巨大的帮助。

提前感谢!


不,没有其他方法。你必须捆绑你的字体并按照Nik所说的做。你也可以查看这个线程:https://dev59.com/33E95IYBdhLWcg3wSb7P - Luis Ollero
访问这个链接,也许对您有所帮助:https://dev59.com/k3E85IYBdhLWcg3wNgvk#16166184 - user2285778
对于需要一个扩展TextView并使其通用的C#/Xamarin解决方案的任何人,即您可以通过xml上的每个TextView属性传递特定的字体路径,请查看此处https://github.com/Cheesebaron/Cheesebaron.FontSample。 - Dave Haigh
8个回答

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

    }

}

2
@mxg 我不明白你说的是什么。 - Chrishan
9
通过覆盖TextView,@mxg 可以在布局文件中使用MyTextView,而无需在活动/片段中进行任何编程操作来实现自定义字体。 - Bill Mote
这里有一个很好的教程:http://www.barebonescoder.com/2010/05/android-development-using-custom-fonts。如果你想考虑不同的字体系列类型,比如加粗或斜体,你需要使用不同的ttf文件。你可以在init方法中用this.getTypeface().getStyle()来处理它,然后为textview使用不同的字体。希望对你有所帮助。 - Milton
以上链接中我只能看到广告和相关链接,没有任何内容,只有广告。 - prateek

27

在您的活动中,就在调用

setContentView(R.id.blahblah);

之后,您应该运行一个方法来遍历整个小部件层次结构并处理字体替换,例如:

setContentView(R.id.blahblah);
Utils.overrideFonts(this, findViewById(android.R.id.content));

提到的 "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
    }
}
在这种方案中,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");
}

如果您使用 Activity 的子类,例如 MyAppActivity(扩展 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));
    }
... ...
}

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

public class SettingsUI extends MyAppActivity {
... ...
} 

希望这能有所帮助... 加油!


6
这是一个适用于简单布局的好解决方案。然而,如果你的活动中有一个ListView,这将不起作用。ListView中的项是在setContent方法之后实例化的,因此列表中的TextView不会受到这个方法的影响。 - Maggie
你是对的,这些需要在...ItemAdapter类中处理。 - lithium
2
操作栏的字体怎么样? - AlikElzin-kilaka
片段中的TextView也不会被修改其字体。 - Simas

9
创建一个样式并将其用于所有文本属性。
<style name="CustomText">
    <item name="android:typeface">YourFontName</item>
</style>

使用方法:
<TextView style="@style/CustomText" />

上面是使用自定义字体在所有活动中进行定制的内容,您可以使用...
Typeface font = Typeface.createFromAsset(getAssets(), "CustomFontName.ttf");  
txt.setTypeface(font);

试试这个。


11
android:typeface字段只允许使用内置字体名称。我该如何分配位于资产文件夹中的自定义字体名称? - Chrishan
3
@hanry,你不能在android:typeface字段中使用自定义字体名称。你回答的第二部分是可以的,但样式部分不行。 - Bill Mote
2
这并没有回答问题:“除了为每个TextView使用settypeface方法,还有其他的方法吗?” - Benjamin Piette

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

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>

FontFactory - 抽象类,扩展它以创建你自己的工厂。

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

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

如果需要更多的代码和样例,请点击这里

。与IT技术有关。


0

好的,你可以做到,但你实际上想要做的是一个字体图集,以一种地图格式运行(它必须按照Unicode顺序运行,该顺序从!"#$% '()x+,-./开始)。函数将接收一个字符串,并计算出每个对应字母在图集中的位置。

这并不太容易,但每个字符必须具有相同的长度和宽度,你也可以使用不同的长度,但那会更加困难。


0
    setContentView(R.layout.activity_main);
    custfont(this, findViewById(android.R.id.content));



private void custfont(final Context context, 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(Typeface.createFromAsset(context.getAssets(), "fonts/ostrichblack.ttf"));
        }
    }
    catch (Exception e) 
    {
    }
}

0

你可以在http://bit.ly/1bzjWQn使用PixlUI。

将他们的 .jar 导入到你的项目中,在 XML 中使用它。

 <com.neopixl.pixlui.components.textview.TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"
    pixlui:typeface="MyFont.ttf" />

别忘了实现方案 ( xmlns:pixlui="http://schemas.android.com/apk/com.neopixl.pixlui" )


1
为什么我们需要添加一些jar包来完成Android本身就能够纯粹完成的工作? - Chrishan
你不能在Android SDK中使用自定义字体。 - odemolliens

0
import android.content.Context;
import android.graphics.Typeface;

/**
 * Created by Sanjeev Kumar on 4/18/2017.
 */

public class FontManager {
    public Context mContext;

    public FontManager(Context context) {
        this.mContext = context;
    }

    public Typeface getSquarkiFont() {
        return Typeface.createFromAsset(mContext.getAssets(), "fonts/Squarki.ttf");
    }

    public Typeface getLazySpringDayFont() {
        return Typeface.createFromAsset(mContext.getAssets(), "fonts/LazySpringDay.ttf");
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接