Ответ 1
Лучшим способом, который я нашел в googling, является: скажем, если вы хотите использовать в TextView, тогда нам нужно расширить Textview и установить шрифт в дальнейшем, мы можем использовать наш настраиваемый Textview в нашем xml. Я покажу расширенный TextView ниже
package com.vins.test;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context) {
super(context);
init();
}
private void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"your_font.ttf");
setTypeface(tf);
}
}
Мы вызываем init() для установки шрифта в каждом из бизнес-структур. Позже мы должны использовать это в нашем main.xml, как показано ниже.
<com.vins.test.MyTextView
android:id="@+id/txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="This is a text view with the font u had set in MyTextView class "
android:textSize="30dip"
android:textColor="#ff0000"
>
Update:
Помните о утечке памяти в пред-4.0 Android, как упоминалось в pandre.