Vâng !!
Câu hỏi này là khá cũ nhưng vẫn còn nếu ai đó đang tìm kiếm câu trả lời (năm 2015) về cách áp dụng phông chữ tùy chỉnh cho tất cả các TextView qua mã xml trực tiếp xem dưới đây:
Đầu tiên:
chúng ta cần phải thêm phông chữ tùy chỉnh thư mục nội dung bên trong thư mục bên trong ứng dụng của bạn:
.ttf hoặc .otf cả công việc trong trường hợp của Android
thứ hai:
Tạo Lớp CustomTextView kéo dài TextView như dưới đây:
public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setTypeface(Typeface tf) {
super.setTypeface(FontCache.getFont(getContext(),"fonts/<font_name>"));
}
}
Thứ ba:
FontCache lớp được sử dụng bên trong setTypeface CustomTextView của() method.Purpose là để làm Caching Font cơ bản sử dụng HashMap:
public class FontCache {
private static Map<String,Typeface> fontMap = new HashMap<String,Typeface>();
public static Typeface getFont(Context context,String fontname){
if(fontMap.containsKey(fontname)){
return fontMap.get(fontname);
}
else{
Typeface tf = Typeface.createFromAsset(context.getAssets(),fontname);
fontMap.put(fontname,tf);
return tf;
}
}
}
Thứ tư: [Bước cuối cùng] Tất cả những gì chúng tôi làm bây giờ là sử dụng CustomTextView trực tiếp bên trong tệp xml của chúng tôi ở bất cứ nơi nào tùy chỉnh phông chữ văn bản được yêu cầu:
<<package_name>.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Font Text"
android:textSize ="18sp"
android:textAppearance="?android:textAppearanceSmall"
android:id="@+id/custom_txt"
/>
Xin lỗi, nếu điều này đã được đăng ở đâu đó trên SO. Chỉ cần nghĩ để chia sẻ nếu nó giúp ai đó !!
Kiểm tra bài đăng này: [http://stackoverflow.com/questions/2888508/how-to-change-the-font-on-the-text-view-in-android](http://stackoverflow.com/question/2888508/how-to-change-the-font-on-the-text-view-in-android) – Praveen
Kiểm tra điều này cũng: http://stackoverflow.com/a/14558090/693752 – Snicolas
http://stackoverflow.com/questions/9030204/how-to-use-custom-font-in-android-xml/9035924#9035924 – Vins