设置自定义字体通过xml
问题描述:
我怎么能设置字体,它的TTF驻留在我的资产
文件夹通过xml?
我知道如何做到这一点编程但你怎么能做到这一点通过XML?先谢谢了。
how can i set a font, whose ttf resides in my assets
folder through xml?
I know how to do that programmatically but how can you do that via xml? Thanks in advance.
答
直接使用XML,但你可以扩展的TextView
并设置默认字体你不能做到这一点。
You cannot do it using XML directly, however you can extend TextView
and set a default font.
package com.nannu;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class NanTV extends TextView{
private Context c;
public NanTV(Context c) {
super(c);
this.c = c;
Typeface tfs = Typeface.createFromAsset(c.getAssets(),
"font/yourfont.ttf");
setTypeface(tfs);
}
public NanTV(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.c = context;
Typeface tfs = Typeface.createFromAsset(c.getAssets(),
"font/yourfont.ttf");
setTypeface(tfs);
// TODO Auto-generated constructor stub
}
public NanTV(Context context, AttributeSet attrs) {
super(context, attrs);
this.c = context;
Typeface tfs = Typeface.createFromAsset(c.getAssets(),
"font/yourfont.ttf");
setTypeface(tfs);
}
}
而在布局中使用新的的TextView
<com.nannu.NanTV
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
我从我的$ P $光伏答案http://*.com/a/11239305/1166537