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


当前回答

首先,默认不是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开发者文档对此有明确的指导

其他回答

最好的实践

TextViewPlus.java:

public class TextViewPlus extends TextView {
    private static final String TAG = "TextView";

    public TextViewPlus(Context context) {
        super(context);
    }

    public TextViewPlus(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomFont(context, attrs);
    }

    public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setCustomFont(context, attrs);
    }

    private void setCustomFont(Context ctx, AttributeSet attrs) {
        TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
        String customFont = a.getString(R.styleable.TextViewPlus_customFont);
        setCustomFont(ctx, customFont);
        a.recycle();
    }

    public boolean setCustomFont(Context ctx, String asset) {
        Typeface typeface = null;
        try {
            typeface = Typeface.createFromAsset(ctx.getAssets(), asset);
        } catch (Exception e) {
            Log.e(TAG, "Unable to load typeface: "+e.getMessage());
            return false;
        }

        setTypeface(typeface);
        return true;
    }
}

attrs.xml:(在哪里放置res/values)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TextViewPlus">
        <attr name="customFont" format="string"/>
    </declare-styleable>
</resources>

使用方法:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:foo="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.mypackage.TextViewPlus
        android:id="@+id/textViewPlus1"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:text="@string/showingOffTheNewTypeface"
        foo:customFont="my_font_name_regular.otf">
    </com.mypackage.TextViewPlus>
</LinearLayout>

希望这对你有所帮助。

最佳实践是使用Android支持库26.0.0或更高版本。

步骤1:添加字体文件

在res文件夹中创建新的字体资源字典 添加字体文件(.ttf, .orf)

例如,当字体文件为helvetica_neue.ttf时,将生成R.font.helvetica_neue

步骤2:创建字体族

在字体文件夹中添加新的资源文件 在元素中包含每个字体文件、样式和权重属性。

例如:

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
    <font
        android:fontStyle="normal"
        android:fontWeight="400"
        android:font="@font/helvetica_neue" />
</font-family>

第三步:使用它

在xml布局中:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/my_font"/>

或添加字体样式:

<style name="customfontstyle" parent="@android:style/TextAppearance.Small">
    <item name="android:fontFamily">@font/lobster</item>
</style>

更多的例子你可以参考文档:

使用字体

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

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

也许可以简单一点:

public class Fonts {
  public static HashSet<String,Typeface> fonts = new HashSet<>();

  public static Typeface get(Context context, String file) {
    if (! fonts.contains(file)) {
      synchronized (this) {
        Typeface typeface = Typeface.createFromAsset(context.getAssets(), name);
        fonts.put(name, typeface);
      }
    }
    return fonts.get(file);
  }
}

// Usage
Typeface myFont = Fonts.get("arial.ttf");

(请注意,此代码未经测试,但通常这种方法应该工作良好。)

首先,默认不是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开发者文档对此有明确的指导