2011-07-19 69 views
23

我正在嘗試執行應用程序範圍內的字體更改並創建樣式文件。在這個文件中(下面)我只是想改變Android的TextAppearance風格的字體值。從Android中的XML文件訪問資產文件夾下的字體

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="NightRiderFont" parent="@android:style/TextAppearance"> 
     <item name="android:typeface"> /***help need here***/ </item> 
    </style> 
</resources> 

但是font是在「assets/fonts /」中。我怎樣才能訪問這種字體,所以我可以使用這種風格作爲主題來擺脫手動編程改變所有TextView。

作爲摘要:如何在XML中訪問'資產文件夾中的文件'?

回答

67

在我的研究中,沒有辦法將外部字體添加到xml文件。只有3種默認字體可用於xml

但是您可以在java中使用此代碼。

Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/verdana.ttf"); 
textfield.setTypeface(tf,Typeface.BOLD); 

更新:

現在我找到一種方法,通過創建擴展TextView的一個自定義類來做到這一點,使用的XML文件。

public class TextViewWithFont extends TextView { 
    private int defaultDimension = 0; 
    private int TYPE_BOLD = 1; 
    private int TYPE_ITALIC = 2; 
    private int FONT_ARIAL = 1; 
    private int FONT_OPEN_SANS = 2; 
    private int fontType; 
    private int fontName; 

    public TextViewWithFont(Context context) { 
     super(context); 
     init(null, 0); 
    } 
    public TextViewWithFont(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(attrs, 0); 
    } 
    public TextViewWithFont(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(attrs, defStyle); 
    } 
    private void init(AttributeSet attrs, int defStyle) { 
     // Load attributes 
     final TypedArray a = getContext().obtainStyledAttributes(
       attrs, R.styleable.font, defStyle, 0); 
     fontName = a.getInt(R.styleable.font_name, defaultDimension); 
     fontType = a.getInt(R.styleable.font_type, defaultDimension); 
     a.recycle(); 
     MyApplication application = (MyApplication) getContext().getApplicationContext(); 
     if (fontName == FONT_ARIAL) { 
      setFontType(application .getArialFont()); 
     } else if (fontName == FONT_OPEN_SANS) { 
      setFontType(application .getOpenSans()); 
     } 
    } 
    private void setFontType(Typeface font) { 
     if (fontType == TYPE_BOLD) { 
      setTypeface(font, Typeface.BOLD); 
     } else if (fontType == TYPE_ITALIC) { 
      setTypeface(font, Typeface.ITALIC); 
     } else { 
      setTypeface(font); 
     } 
    } 
} 

和XML

<com.example.customwidgets.TextViewWithFont 
     font:name="Arial" 
     font:type="bold" 
     android:layout_width="wrap_content" 
     android:text="Hello world " 
     android:padding="5dp" 
     android:layout_height="wrap_content"/> 

不要忘記添加架構根你的XML

xmlns:font="http://schemas.android.com/apk/res-auto" 

,並創建一個attrs.xml文件中values目錄,也就是抱着我們的定製attribues :

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="font"> 
     <attr name="type"> 
     <enum name="bold" value="1"/> 
      <enum name="italic" value="2"/> 
     </attr> 
     <attr name="name"> 
      <enum name="Arial" value="1"/> 
      <enum name="OpenSans" value="2"/> 
     </attr> 
    </declare-styleable> 
</resources> 

更新:

時發現該自定義視圖在列表視圖 使用的一些性能問題,這是因爲字體對象是創建每次 視圖加載時間。解決方法我發現是初始化應用 類的字體和

MyApplication application = (MyApplication) getContext().getApplicationContext(); 

應用類是指該字體對象看起來像這樣

public class MyApplication extends Application { 

    private Typeface arialFont, openSans; 

    public void onCreate() { 
     super.onCreate(); 

     arialFont = Typeface.createFromAsset(getAssets(), QRUtils.FONT_ARIAL); 
     openSans = Typeface.createFromAsset(getAssets(), QRUtils.FONT_OPEN_SANS); 
    } 

    public Typeface getArialFont() { 
     return arialFont; 
    } 

    public Typeface getOpenSans() { 
     return openSans; 
    } 
} 
+0

是的,它是痛苦的改變所有單一的字體,但似乎沒有別的事情要做。 – erkangur

+0

我知道。我之前也有同樣的情況。 –

+0

謝謝,這對我很有用。但是有沒有人遇到官方文檔,爲什麼你不能在XML文件中設置自定義字體? – toobsco42

2

索里亞是正確的,但如果你必須把在許多textView上使用相同的字體,我建議您將該代碼放入一個返回字體所需的靜態方法中。它會減少代碼中的行數。或者甚至更好地創建一個擴展Application並創建一個返回Typeface的GET方法的類。該方法可從應用程序中的任何Activity訪問(無需使用靜態變量或靜態方法)。

+0

是的..你是對的 –

0

希望使用全給你: -

TextView text = (TextView) findViewById(R.id.custom_font); 
Typeface font = Typeface.createFromAsset(getAssets(), "yourfont.ttf"); 
text.setTypeface(font); 
6

編輯2:https://developer.android.com/preview/features/fonts-in-xml.html


最後字體由XML(也可以通過支持庫向後兼容)的支持編輯:

我現在使用Calligraphy庫。這是自定義字體最簡單的方法。

你可以做的:在TextView

  • 自定義字體

    • 自定義字體在TextAppearance
    • 自定義字體的樣式在主題
    • 自定義字體
    • FontSpannable只適用字體部分文字

    我找到了另一種方法來做到這一點。

    您在使用本tutorial

    它並不難,使自己的TextView並在此之後,你可以只使用TextView用自己的字體。

    我不知道是否還有人看這個,但我認爲這可能有幫助。

  • +0

    感謝凱文,它岩石! –

    4

    如果您使用的是單一字體,請使用此功能。

    public static void applyFont(final Context context, final View root, final String fontName) { 
         try { 
          if (root instanceof ViewGroup) { 
           ViewGroup viewGroup = (ViewGroup) root; 
           for (int i = 0; i < viewGroup.getChildCount(); i++) 
            applyFont(context, viewGroup.getChildAt(i), fontName); 
          } else if (root instanceof TextView) 
           ((TextView) root).setTypeface(Typeface.createFromAsset(context.getAssets(), fontName)); 
         } catch (Exception e) { 
          Log.e("ProjectName", String.format("Error occured when trying to apply %s font for %s view", fontName, root)); 
          e.printStackTrace(); 
         } 
        } 
    
    +0

    好....................謝謝 –

    +2

    它的表現如何? – atasoyh

    +0

    我應該在哪裏調用你的函數? –

    -2

    您可以從資產文件夾訪問您的字體文件到xml文件。

    機器人:fontFamily中= 「字體/ roboto_regular.ttf」

    字體是資產的文件夾的子文件。

    1

    源泉變化是Android的非常基本的功能,其中大部分是需要每application.so每一個想改變只有在應用程序,減少我們的開發時間一次,所以我會建議你看看這個鏈接 FountChanger.class

    0

    你可以使用這個庫:https://github.com/chrisjenx/Calligraphy

    你只需要添加你要在你的佈局中使用的字體。 像這樣:

    <TextView 
    fontPath="fonts/verdana.ttf" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 
    
    0

    你剛纔讓公共電話和連接方法這樣

    public class TextViewFontType { 
    
    public Typeface typeface; 
    
    public void fontTextView(final TextView textView, final String fonttype) { 
        typeface = Typeface.createFromAsset(textView.getContext().getAssets(), fonttype); 
        textView.setTypeface(typeface); 
    } 
    

    有你使用任何方法在那裏設置的TextView FONT-FAMILY。

    public class FontList { 
    
        public static final String gothicbNormal="fonts/gothicb.ttf"; 
        public static final String gothicbBold="fonts/gothicb.ttf"; 
    } 
    

    在您剛剛調用任何方法與傳遞兩個參數後,FontList calss。

    0

    我把機器人孩子的答案,並使它與任何字體的工作,只需直接輸入字體文件名到XML:

    layout.xml

    <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:custom="http://schemas.android.com/apk/res-auto" > 
    
        <!-- where font file is at "assets/fonts/arial.ttf" --> 
        <com.odbol.widgets.TextViewWithFont 
         ... 
         custom:fontFilePath="fonts/arial.ttf" /> 
    
    </RelativeLayout> 
    

    Fonts.java

    public class Fonts { 
    
        // use Weak so fonts are freed from memory when you stop using them 
        private final static Map<String, Typeface> fonts = new WeakHashMap<>(5); 
    
        /*** 
        * Returns a font at the given path within the assets directory. 
        * 
        * Caches fonts to save resources. 
        * 
        * @param context 
        * @param fontPath Path to a font file relative to the assets directory, e.g. "fonts/Arial.ttf" 
        * @return 
        */ 
        public synchronized static Typeface getFont(Context context, String fontPath) { 
         Typeface font = fonts.get(fontPath); 
    
         if (font == null) { 
          font = Typeface.createFromAsset(context.getAssets(), fontPath); 
          fonts.put(fontPath, font); 
         } 
    
         return font; 
        } 
    } 
    

    values/attrs.xml

    <resources> 
    
        <declare-styleable name="TextViewWithFont"> 
         <!-- a path to a font, relative to the assets directory --> 
         <attr name="fontFilePath" format="string" /> 
    
         <attr name="type"> 
          <enum name="bold" value="1"/> 
          <enum name="italic" value="2"/> 
         </attr> 
        </declare-styleable> 
    </resources> 
    

    TextViewWithFont.java

    public class TextViewWithFont extends TextView { 
        private int defaultDimension = 0; 
        private int TYPE_BOLD = 1; 
        private int TYPE_ITALIC = 2; 
        private int fontType; 
        private int fontName; 
    
        public TextViewWithFont(Context context) { 
         super(context); 
         init(null, 0); 
        } 
        public TextViewWithFont(Context context, AttributeSet attrs) { 
         super(context, attrs); 
         init(attrs, 0); 
        } 
        public TextViewWithFont(Context context, AttributeSet attrs, int defStyle) { 
         super(context, attrs, defStyle); 
         init(attrs, defStyle); 
        } 
        private void init(AttributeSet attrs, int defStyle) { 
         // Load attributes 
         final TypedArray a = getContext().obtainStyledAttributes(
           attrs, R.styleable.TextViewWithFont, defStyle, 0); 
         String fontPath = a.getString(R.styleable.TextViewWithFont_fontFilePath); 
         fontType = a.getInt(R.styleable.TextViewWithFont_type, defaultDimension); 
         a.recycle(); 
    
         if (fontPath != null) { 
          setFontType(Fonts.getFont(getContext(), fontPath)); 
         } 
        } 
        private void setFontType(Typeface font) { 
         if (fontType == TYPE_BOLD) { 
          setTypeface(font, Typeface.BOLD); 
         } else if (fontType == TYPE_ITALIC) { 
          setTypeface(font, Typeface.ITALIC); 
         } else { 
          setTypeface(font); 
         } 
        } 
    } 
    
    0

    1.Fisrt我們可以添加文件夾資產>,在您的應用程序的字體在隨後
    2.write接入代碼的字體styles.ttfs字符串如:

    <string name="fontregular">OpenSans-Light.ttf</string> 
    <string name="fontmedium">OpenSans-Regular.ttf</string> 
    

    3.訪問一些佈局xml文件textview代碼,如下所示:

     <EditText 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:fontFamily="@string/fontregular" 
         android:textColor="@color/normalfont" 
         android:textSize="@dimen/textregular"/> 
    

    例如,字體大小4.增加尺寸:

    <dimen name="textregular">14sp</dimen> 
    <dimen name="textheader">16sp</dimen> 
    <dimen name="smalltext">12sp</dimen> 
    <dimen name="littletext">10sp</dimen> 
    <dimen name="hightfont">18sp</dimen> 
    

    5.Add字體顏色中的顏色:

    <color name="normalfont">#666</color> 
    <color name="headerfont">#333</color> 
    <color name="aquablue">#4da8e3</color> 
    <color name="orange">#e96125</color> 
    

    6.Then應用更改很容易處理以改變孔的應用。 快樂編碼保持微笑..

    相關問題