Tôi đang định nghĩa XML kiểu cho ứng dụng Android của mình. Tôi có một số tập tin TTF tôi muốn sử dụng, làm thế nào tôi có thể thiết lập kiểu chữ để sử dụng những tập tin như phông chữ trái với "sans" chung, "serif" & "monospace". Cảm ơnĐặt phông chữ cụ thể trong một styles.xml
Trả lời
Bạn chỉ có thể sử dụng phông chữ tùy chỉnh thông qua mã Java, không phải thông qua XML bố cục hoặc kiểu/chủ đề - xin lỗi!
xem APIDemos Ví dụ trong Tài liệu.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont" parent="@android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
</style>
</resources>
Tôi tạo ra một chút hướng dẫn về vấn đề này ... Tôi hy vọng nó có thể hữu ích http://spinettaro.blogspot.it/2013/01/custom-font-for-view.html
TextViewPlus.java:
package com.example;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
public class TextViewPlus extends TextView {
private static final String TAG = "TextView";
public TextViewPlus(Context context) {
super(context);
}
public TextViewPlus(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
String customFont = a.getString(R.styleable.TextViewPlus_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(), asset);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: "+e.getMessage());
return false;
}
setTypeface(tf);
return true;
}
}
attrs.xml: (trong res/values)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TextViewPlus">
<attr name="customFont" format="string"/>
</declare-styleable>
</resources>
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:foo="http://schemas.android.com/apk/res/com.example"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.example.TextViewPlus
android:id="@+id/textViewPlus1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="@string/showingOffTheNewTypeface"
foo:customFont="saxmono.ttf">
</com.example.TextViewPlus>
</LinearLayout>
Bạn sẽ đặt "saxmono.ttf" trong tài sản thư mục.
Có, bạn có thể :)
Bước 1: Tạo thư mục và đặt tên là 'font' và .ttf bên trong thư mục. enter image description here
bước 2: Đến style.xml và làm như sau: - enter image description here
bước 3: Sử dụng các tag style trong bất kỳ đối tượng UI: -
Trước khi tất cả điều này, trước tiên, bạn cần phải cập nhật để cập nhật để hỗ trợ thư viện 26 đây là một liên kết https://segunfamisa.com/posts/custom-fonts-with-android-support-library –
Thanks a lot Tôi đánh giá cao nó. – NickTFried
@ SergiCastellsaguéMillán: Câu hỏi này dành cho việc sử dụng phông chữ TTF. Câu trả lời bạn liên kết là dành cho mọi thứ ** nhưng ** phông chữ TTF. – CommonsWare
Vì đây là câu trả lời cũ; bạn có biết liệu điều này có thay đổi gì trong các phiên bản sau không, @CommonsWare? Tôi cũng rất thích có thể thay đổi phông chữ bằng cách xác định phông chữ trong tài liệu ''. –