2

如何更改 Android TextView 中的字体类型?

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Welcome!"
    android:textSize="20sp"  />
4

3 回答 3

6

您可以使用该android:typeface属性。例如:

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Welcome!"
    android:typeface="sans"
    android:textSize="20sp"  />

XML 属性只允许值normalsansserifmonospace。如果您想使用不同的Typeface(可能是您的应用程序附带的自定义),您必须在setTypeface()加载 TextView 后调用视图来在代码中执行此操作。

于 2012-09-23T15:49:34.653 回答
6

要添加到Ted Hopp 的答案,如果您正在查看您的自定义字体,请TextViewActivity您引用的地方TextView使用此代码示例:

Typeface blockFonts = Typeface.createFromAsset(getAssets(),"ROBOTO-MEDIUM_0.TTF");
TextView txtSampleTxt = (TextView) findViewById(R.id.your_textview_id);
txtSampleTxt.setTypeface(blockFonts);

虽然,在您使用它之前,您需要将您选择的字体复制到assets文件夹中。

您还可以查看我在 SO 上的一个答案,其中有几个网站可以用来下载字体。他们是:

http://www.google.com/webfonts

http://www.fontsquirrel.com/

于 2012-09-23T16:04:35.987 回答
0

您可以通过将字体文件(example.tff)样式放在 Assets/fonts 中以编程方式设置所需字体,然后将您自己的字符串放入 fontpath 变量

    String fontPath="fonts/Unkempt-Regular.ttf";
    TextView aboutus=(TextView)findViewById(R.id.aboutus);
    Typeface type=Typeface.createFromAsset(getAssets(), fontPath);
    aboutus.setTypeface(type);
于 2014-01-24T07:53:03.683 回答