2014-11-01 54 views
4

我已经在我的资产文件夹中创建了一个名为fonts的文件夹,并在其中放置了一个.ttf文件。并指定该字体家庭像我这样的文字在xml文件中定义资产文件夹中的字体android

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" 
     /> 

回答

0

只需使用此代码。只需简单地添加segoeui.ttf与文件名。

android:fontFamily="fonts/segoeui.ttf" 
+0

它的工作,但我这不是在工作作风和苛刻的API级别至少16个,但我不能去了超过14 <项目名称=“机器人:fontFamily中”>字体/ segoeui.ttf 是否有任何possibilty解决这个问题? – Yawar 2014-11-01 14:18:33

+0

我面临同样的问题。任何方案? – Nithinjith 2016-05-13 08:18:58

+0

您是否尝试过我的解决方案? – 2016-05-15 05:46:22

-1

你可以做到这一点,方法是将该如下图所示

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); 
} 

} 

但要记住扩展的TextView这样CustomTextView,存在较旧的Android版本严重的内存concers。将这一问题

https://code.google.com/p/android/issues/detail?id=9904

0

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

+0

这是不正确的答案。尝试扩展'Application'类。 – 2015-12-08 05:40:14

0

启发你可以按照这个。 在您的应用程序类:

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; 
    } 

之后,你可以在XML中使用按在你的问题中提到。

相关问题