Trong nghiên cứu của tôi, không có cách nào để thêm phông chữ bên ngoài vào tệp xml. Chỉ có 3 phông chữ mặc định có sẵn trong xml
Nhưng bạn có thể sử dụng trong java bằng cách sử dụng mã này.
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/verdana.ttf");
textfield.setTypeface(tf,Typeface.BOLD);
Cập nhật:
Bây giờ tôi tìm thấy một cách để làm điều này bằng cách tạo một lớp tùy chỉnh mở rộng TextView và sử dụng trong file xml.
public class TextViewWithFont extends TextView {
private int defaultDimension = 0;
private int TYPE_BOLD = 1;
private int TYPE_ITALIC = 2;
private int FONT_ARIAL = 1;
private int FONT_OPEN_SANS = 2;
private int fontType;
private int fontName;
public TextViewWithFont(Context context) {
super(context);
init(null, 0);
}
public TextViewWithFont(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs, 0);
}
public TextViewWithFont(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs, defStyle);
}
private void init(AttributeSet attrs, int defStyle) {
// Load attributes
final TypedArray a = getContext().obtainStyledAttributes(
attrs, R.styleable.font, defStyle, 0);
fontName = a.getInt(R.styleable.font_name, defaultDimension);
fontType = a.getInt(R.styleable.font_type, defaultDimension);
a.recycle();
MyApplication application = (MyApplication) getContext().getApplicationContext();
if (fontName == FONT_ARIAL) {
setFontType(application .getArialFont());
} else if (fontName == FONT_OPEN_SANS) {
setFontType(application .getOpenSans());
}
}
private void setFontType(Typeface font) {
if (fontType == TYPE_BOLD) {
setTypeface(font, Typeface.BOLD);
} else if (fontType == TYPE_ITALIC) {
setTypeface(font, Typeface.ITALIC);
} else {
setTypeface(font);
}
}
}
và trong xml
<com.example.customwidgets.TextViewWithFont
font:name="Arial"
font:type="bold"
android:layout_width="wrap_content"
android:text="Hello world "
android:padding="5dp"
android:layout_height="wrap_content"/>
không quên để thêm lược đồ trong thư mục gốc của xml của bạn
xmlns:font="http://schemas.android.com/apk/res-auto"
Và tạo ra một tập tin attrs.xml
bên values
thư mục, mà đang nắm giữ attribues tùy chỉnh của chúng tôi :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="font">
<attr name="type">
<enum name="bold" value="1"/>
<enum name="italic" value="2"/>
</attr>
<attr name="name">
<enum name="Arial" value="1"/>
<enum name="OpenSans" value="2"/>
</attr>
</declare-styleable>
</resources>
Cập nhật:
Tìm thấy một số vấn đề hiệu suất khi xem phong tục này được sử dụng trong listview, đó là bởi vì các đối tượng chữ đang tạo ra mỗi khi xem được tải. Giải pháp tôi thấy là để khởi tạo phông chữ trong ứng dụng Class và tham khảo đối tượng chữ bằng cách
MyApplication application = (MyApplication) getContext().getApplicationContext();
lớp Application sẽ trông như thế này
public class MyApplication extends Application {
private Typeface arialFont, openSans;
public void onCreate() {
super.onCreate();
arialFont = Typeface.createFromAsset(getAssets(), QRUtils.FONT_ARIAL);
openSans = Typeface.createFromAsset(getAssets(), QRUtils.FONT_OPEN_SANS);
}
public Typeface getArialFont() {
return arialFont;
}
public Typeface getOpenSans() {
return openSans;
}
}
Vâng, thật khó để thay đổi tất cả phông chữ đơn, nhưng có vẻ như không có gì khác để làm. – erkangur
Ya tôi biết. Tôi cũng có tình huống tương tự trước đây. –
Cảm ơn, điều này đã làm việc cho tôi. Nhưng có ai đi qua tài liệu chính thức là tại sao bạn không thể đặt một kiểu chữ tùy chỉnh trong một tệp XML? – toobsco42