mainactivity如下:
package cn.testfont;
import android.os.bundle;
import android.widget.textview;
import android.app.activity;
import android.graphics.typeface;
/**
* demo描述:
* 利用ttf字型檔案文字的顯示效果
*
* 步驟如下:
* 1 在asset下建立fonts檔案夾
* 2 将.ttf檔案拖入fonts檔案夾typeface
* 3 在代碼中為textview設定
*
*/
public class mainactivity extends activity {
private textview mtextview;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
init();
}
private void init(){
mtextview=(textview) findviewbyid(r.id.textview);
typeface typeface = typeface.createfromasset(getassets(),"fonts/test.ttf");
mtextview.settypeface(typeface);
}
main.xml如下:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<textview
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9527"
android:textsize="22sp"
android:layout_centerinparent="true"
/>
</relativelayout>