您可以創建自定義FontTextView: - 添加在SRC包這一習俗FontTextView:
package com.example.android.ui;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
import java.util.HashMap;
import java.util.Map;
public class FontTextView extends TextView {
private static Map<String, Typeface> mTypefaces;
public FontTextView(final Context context) {
this(context, null);
}
public FontTextView(final Context context, final AttributeSet attrs) {
this(context, attrs, 0);
}
public FontTextView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
if (mTypefaces == null) {
mTypefaces = new HashMap<String, Typeface>();
}
final TypedArray array = context.obtainStyledAttributes(attrs, styleable.FontTextView);
if (array != null) {
final String typefaceAssetPath = array.getString(
R.styleable.FontTextView_customTypeface);
if (typefaceAssetPath != null) {
Typeface typeface = null;
if (mTypefaces.containsKey(typefaceAssetPath)) {
typeface = mTypefaces.get(typefaceAssetPath);
} else {
AssetManager assets = context.getAssets();
typeface = Typeface.createFromAsset(assets, typefaceAssetPath);
mTypefaces.put(typefaceAssetPath, typeface);
}
setTypeface(typeface);
}
array.recycle();
}
}
}
-In RES /值加tt_attrs.xml
<resources>
<declare-styleable name="FontTextView">
<attr name="customTypeface" format="string" />
</declare-styleable>
</resources>
-I n您的佈局要添加字體的TextView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:geekui="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.android.ui.FontTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textSize="30sp"
geekui:customTypeface="fonts/segoeui.ttf" />
<com.entreprise.android.ui.FontTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="@string/app_name"
geekui:customTypeface="fonts/Times New Roman.ttf" />
<com.entreprise.android.ui.FontTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="@string/app_name"
geekui:customTypeface="fonts/arial unicode ms.ttf" />
</LinearLayout>
這一切的源代碼是從https://github.com/ragunathjawahar/android-typeface-textview
它的工作,但我這不是在工作作風和苛刻的API級別至少16個,但我不能去了超過14 <項目名稱=「機器人:fontFamily中」>字體/ segoeui.ttf 是否有任何possibilty解決這個問題? – Yawar 2014-11-01 14:18:33
我面臨同樣的問題。任何方案? – Nithinjith 2016-05-13 08:18:58
您是否嘗試過我的解決方案? – 2016-05-15 05:46:22