您可以创建自定义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