在android系统中,自带的只有三种字体---
"sans","serif"和"monospace",你只要在xml文件中的
android:typeface
属性使用它们:
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:text="sans:"
android:layout_marginRight="4px"
android:textSize="20sp"
/>
<TextView
android:id="@+id/sans"
android:text="Hello, world!"
android:typeface="sans"
android:textSize="20sp"
/>
</TableRow>
<TableRow>
<TextView
android:text="serif:"
android:layout_marginRight="4px"
android:textSize="20sp"
/>
<TextView
android:id="@+id/serif"
android:text="Hello, world!"
android:typeface="serif"
android:textSize="20sp"
/>
</TableRow>
<TableRow>
<TextView
android:text="monospace:"
android:layout_marginRight="4px"
android:textSize="20sp"
/>
<TextView
android:id="@+id/monospace"
android:text="Hello, world!"
android:typeface="monospace"
android:textSize="20sp"
/>
</TableRow>
<TableRow>
<TextView
android:text="Custom:"
android:layout_marginRight="4px"
android:textSize="20sp"
/>
<TextView
android:id="@+id/custom"
android:text="Hello, world!"
android:textSize="20sp"
/>
</TableRow>
</TableLayout>
有时候,系统自带的字体并不能满足我们特殊的需求,这时候就需要引用其他的字体了,可以把下载的字体文件放在assets目录下.自定义字体文件不能使用xml代码读取而应该使用java代码:
public class Test extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
TextView tv=(TextView)findViewById(R.id.custom);
Typeface face=Typeface.createFromAsset(getAssets(),
"fonts/HandmadeTypewriter.ttf");//也可以使用其他的静态方法获取
tv.setTypeface(face);
}
}
这个ttf文件在
http://moorstation.org/typoasis/designers/klein07/text01/handmade.htm。
注意:如果你下载的字体Android不支持,那么系统会使用默认字体"sans"。由于应用自定义字体需要额外空间,所以一般不是都需要自定义字体的.
分享到:
相关推荐
Smart Fonts for Android:轻轻松松使用 Android 自定义字体
Android 自定义字体Fontinator是一个让使用自定义字体更方便的安卓库。
该android工程可直接导入eclipse,通过菜单切换字体
比较简洁的启动界面的实现,包括自定义字体和加载动画。
android自定义seekbar刻度条控制网页字体大小
通过自定义style,从主题中动态设置app全局字体的Demo。
主要介绍了Android使用自定义字体的方法,涉及Android字体设置的步骤与相关注意事项,需要的朋友可以参考下
Android SpannableString实现TextView不同大小文字居中显示。
android spinner 自定义 字体大小