2014-11-01 51 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中使用按在你的問題中提到。

相關問題