如何更改 Android TextView 中的字体类型?
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome!"
android:textSize="20sp" />
如何更改 Android TextView 中的字体类型?
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome!"
android:textSize="20sp" />
您可以使用该android:typeface
属性。例如:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome!"
android:typeface="sans"
android:textSize="20sp" />
XML 属性只允许值normal
、sans
、serif
和monospace
。如果您想使用不同的Typeface
(可能是您的应用程序附带的自定义),您必须在setTypeface()
加载 TextView 后调用视图来在代码中执行此操作。
要添加到Ted Hopp 的答案,如果您正在查看您的自定义字体,请TextView
在Activity
您引用的地方TextView
使用此代码示例:
Typeface blockFonts = Typeface.createFromAsset(getAssets(),"ROBOTO-MEDIUM_0.TTF");
TextView txtSampleTxt = (TextView) findViewById(R.id.your_textview_id);
txtSampleTxt.setTypeface(blockFonts);
虽然,在您使用它之前,您需要将您选择的字体复制到assets
文件夹中。
您还可以查看我在 SO 上的一个答案,其中有几个网站可以用来下载字体。他们是:
您可以通过将字体文件(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);