xmlから含めることはできません。あなたはそのようなコードを通してそれをしなければなりません:
final TextView myTextView = getTextView();
final AssetManager assets = ctx.getAssets();
final Typeface font = Typeface.createFromAsset(assets, "thamoa.ttf");
setTypeface(font);
気の利いたトリックの1つは、TextViewを拡張し、実行時にフォントを自動適用することです。
public class CustomTextView extends TextView {
public CustomTextView(Context ctx) {
super(ctx);
setupText(ctx);
}
public CustomTextView(Context ctx, AttributeSet attrs) {
super(ctx, attrs);
setupText(ctx);
}
public CustomTextView(Context ctx, AttributeSet attrs, int defStyle) {
super(ctx, attrs, defStyle);
setupText(ctx);
}
private void setupText(Context ctx) {
// check if in edit mode and return. Fonts can't be applied when viewing from editor
if(isInEditMode()) {
return;
}
final AssetManager assets = ctx.getAssets();
final TypeFace font = Typeface.createFromAsset(assets, "thamoa.ttf");
setTypeface(font);
}
}
次に、同じように使用できますが、xmlでそのように参照します。
<package.containing.class.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<!-- whatever attributes that would normally apply here -->
/>