如何改变字体在TextView,默认它显示为Arial?怎么改成Helvetica字体?


当前回答

从资产中获取字体并设置为所有子元素

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(Typeface.createFromAsset(context.getAssets(),"DroidNaskh.ttf"));// "BKOODB.TTF"));
        }
    } catch (Exception e) {
 }
 } 

其他回答

首先,默认不是Arial。默认为Droid Sans。

其次,要更改为不同的内置字体,请在布局XML中使用android:typeface或在Java中使用setTypeface()。

第三,Android中没有Helvetica字体。内置选项有Droid Sans (Sans)、Droid Sans Mono (monospace)和Droid Serif (Serif)。虽然您可以将自己的字体与应用程序捆绑在一起,并通过setTypeface()使用它们,但请记住,字体文件很大,在某些情况下需要许可协议(例如Helvetica,一种Linotype字体)。

EDIT

The Android design language relies on traditional typographic tools such as scale, space, rhythm, and alignment with an underlying grid. Successful deployment of these tools is essential to help users quickly understand a screen of information. To support such use of typography, Ice Cream Sandwich introduced a new type family named Roboto, created specifically for the requirements of UI and high-resolution screens. The current TextView framework offers Roboto in thin, light, regular and bold weights, along with an italic style for each weight. The framework also offers the Roboto Condensed variant in regular and bold weights, along with an italic style for each weight.

ICS之后,android包含了Roboto字体样式, 阅读更多Roboto

编辑2

随着支持库26的出现,Android现在支持自定义字体 违约。您可以在res/fonts中插入新的字体,这些字体可以单独以XML或编程方式设置为TextViews。整个应用程序的默认字体也可以通过定义它styles.xml来改变,android开发者文档对此有明确的指导

首先下载所需字体的.ttf文件(arial.ttf)。把它放在资产文件夹中。(在assets文件夹内创建名为fonts的新文件夹,并将其放置在其中。)使用以下代码应用字体到你的TextView:

Typeface type = Typeface.createFromAsset(getAssets(),"fonts/arial.ttf"); 
textView.setTypeface(type);

你可能想要创建一个包含所有字体的静态类。这样,您就不会多次创建字体,这可能会严重影响性能。 只要确保你在“assets”文件夹下创建了一个名为“fonts”的子文件夹。

你可以这样做:

public class CustomFontsLoader {

public static final int FONT_NAME_1 =   0;
public static final int FONT_NAME_2 =   1;
public static final int FONT_NAME_3 =   2;

private static final int NUM_OF_CUSTOM_FONTS = 3;

private static boolean fontsLoaded = false;

private static Typeface[] fonts = new Typeface[3];

private static String[] fontPath = {
    "fonts/FONT_NAME_1.ttf",
    "fonts/FONT_NAME_2.ttf",
    "fonts/FONT_NAME_3.ttf"
};


/**
 * Returns a loaded custom font based on it's identifier. 
 * 
 * @param context - the current context
 * @param fontIdentifier = the identifier of the requested font
 * 
 * @return Typeface object of the requested font.
 */
public static Typeface getTypeface(Context context, int fontIdentifier) {
    if (!fontsLoaded) {
        loadFonts(context);
    }
    return fonts[fontIdentifier];
}


private static void loadFonts(Context context) {
    for (int i = 0; i < NUM_OF_CUSTOM_FONTS; i++) {
        fonts[i] = Typeface.createFromAsset(context.getAssets(), fontPath[i]);
    }
    fontsLoaded = true;

}
}

通过这种方式,您可以从应用程序中的任何地方获取字体。

Typeface tf = Typeface.createFromAsset(getAssets(),
        "fonts/DroidSansFallback.ttf");
TextView tv = (TextView) findViewById(R.id.CustomFontText);
tv.setTypeface(tf);

以上答案是正确的。只要确保你在“assets”文件夹下创建了一个名为“fonts”的子文件夹,如果你正在使用这段代码。