Android 从xml文件中的资产文件夹定义字体
我已经在我的资产文件夹中创建了名为fonts的文件夹,并在其中放置了一个.ttf文件。像这样将fontfamily指定给我的文本Android 从xml文件中的资产文件夹定义字体,android,xml,fonts,android-assets,Android,Xml,Fonts,Android Assets,我已经在我的资产文件夹中创建了名为fonts的文件夹,并在其中放置了一个.ttf文件。像这样将fontfamily指定给我的文本 txtName.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/segoeui.ttf")); 但是我想在xml文件中指定这个字体。可能是这样的 <TextView android:id="@+id/txtName" style="@style/My
txtName.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/segoeui.ttf"));
但是我想在xml文件中指定这个字体。可能是这样的
<TextView
android:id="@+id/txtName"
style="@style/My_Name"
android:fontFamily="@android:asset/fonts/segoeui"
/>
只需使用此代码即可。只需添加带有文件名的
segoeui.ttf
android:fontFamily="fonts/segoeui.ttf"
您可以通过使CustomTextView像下面所示那样扩展TextView来实现这一点
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context) {
super(context);
init();
}
private void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"your_font.ttf");
setTypeface(tf);
}
}
但请记住,旧版本的android存在严重的内存问题。请参阅此问题
您可以创建自定义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();
}
}
}
package com.example.android.ui;
导入android.content.Context;
导入android.content.res.AssetManager;
导入android.content.res.TypedArray;
导入android.graphics.Typeface;
导入android.util.AttributeSet;
导入android.widget.TextView;
导入java.util.HashMap;
导入java.util.Map;
公共类FontTextView扩展了TextView{
私有静态映射面;
公共FontTextView(最终上下文){
这个(上下文,空);
}
公共FontTextView(最终上下文、最终属性集属性){
这(上下文,属性,0);
}
公共FontTextView(最终上下文、最终属性集属性、最终int-defStyle){
超级(上下文、属性、定义样式);
如果(mTypefaces==null){
mTypefaces=newhashmap();
}
最终类型Darray数组=context.actainStyledAttributes(attrs,styleable.fontextView);
if(数组!=null){
最终字符串typefaceAssetPath=array.getString(
R.styleable.FontTextView_自定义字体);
如果(typefaceAssetPath!=null){
字体=空;
if(mTypefaces.containsKey(typefaceAssetPath)){
typeface=mTypefaces.get(typefaceAssetPath);
}否则{
AssetManager资产=context.getAssets();
typeface=typeface.createFromAsset(资产,typeface资产路径);
mTypefaces.put(typefaceAssetPath,typeface);
}
设置字体(字体);
}
array.recycle();
}
}
}
-在res/values中添加tt_attrs.xml
<resources>
<declare-styleable name="FontTextView">
<attr name="customTypeface" format="string" />
</declare-styleable>
</resources>
您可以按此操作。
在应用程序类中:
Typeface fontHelvetica = Typeface.createFromAsset(this.getResources().getAssets(), "font/helvetica_font.ttf");
injectTypeface("helvetica", fontHelvetica);
private boolean injectTypeface(String fontFamily, Typeface typeface) {
try {
Field field = Typeface.class.getDeclaredField("sSystemFontMap");
field.setAccessible(true);
Object fieldValue = field.get(null);
Map<String, Typeface> map = (Map<String, Typeface>) fieldValue;
map.put(fontFamily, typeface);
return true;
} catch (Exception e) {
Log.e("Font-Injection", "Failed to inject typeface.", e);
}
return false;
}
Typeface fontHelvetica=Typeface.createFromAsset(this.getResources().getAssets(),“font/helvetica_font.ttf”);
字体(“helvetica”,fontHelvetica);
专用布尔字体(字符串字体系列,字体){
试一试{
Field=Typeface.class.getDeclaredField(“sSystemFontMap”);
字段。setAccessible(true);
Object fieldValue=field.get(null);
Map Map=(Map)字段值;
map.put(字体、字体);
返回true;
}捕获(例外e){
Log.e(“字体注入”,“未能注入字体”,e);
}
返回false;
}
之后,您可以按照问题中提到的使用XML。在应用程序中包含ttf文件会增加应用程序的大小
可下载字体是使用其他字体的最佳解决方案。
使用以下参考资料:
文档:
示例:
您仍然希望包含fontFile,然后使用以下答案:
它可以工作,但我认为这不符合风格,要求API级别至少为16,但我不能超过14种字体/segoui.ttf有没有可能解决这个问题?我面临着同样的问题。有什么解决办法吗?@adnan amjad不幸的是,我不能让这个工作。是否需要外部库(如Caligraphy)才能使其正常工作?这不是正确答案。尝试扩展应用程序
类。
Typeface fontHelvetica = Typeface.createFromAsset(this.getResources().getAssets(), "font/helvetica_font.ttf");
injectTypeface("helvetica", fontHelvetica);
private boolean injectTypeface(String fontFamily, Typeface typeface) {
try {
Field field = Typeface.class.getDeclaredField("sSystemFontMap");
field.setAccessible(true);
Object fieldValue = field.get(null);
Map<String, Typeface> map = (Map<String, Typeface>) fieldValue;
map.put(fontFamily, typeface);
return true;
} catch (Exception e) {
Log.e("Font-Injection", "Failed to inject typeface.", e);
}
return false;
}