Android利用TTF文件设置字体

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>


posted @ 2017-05-04 14:08  jzdwajue  阅读(746)  评论(0编辑  收藏  举报