Trước hết chúng ta cần có các file định dạng fonts và copy nó vào thư mục assets
Sau đó tại file *.java ta dùng lệnh Typeface.createFromAsset để đặt fonts cho text.

Ví dụ đặt font cho TextView:
file *.xml

fonts in android

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Font" >

    <TextView
        android:id="@+id/myFont"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="35dp"
        android:text="nguyenvanquan7826"
        android:textColor="#ff0000"
        android:textSize="23sp" />

</RelativeLayout>

file *.java

package VSNet.Test;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Typeface;
import android.widget.TextView;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		TextView font = (TextView) findViewById(R.id.myFont);
		Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/VALLERGO.otf"); // dat fonts
		font.setTypeface(myTypeface);//kich hoat fonts
	}
}

Kết quả :

fonts android

Download file Demo: Android_Fonts

Advertisements