2014-09-13 19 views
0

我有以下麥粒腫代碼設置,設置字體的Android本地

<!-- Application theme. --> 
    <style name="AppTheme" parent="AppBaseTheme"> 
     <!-- All customizations that are NOT specific to a particular API-level can go here. --> 
     <item name="android:textViewStyle">@style/RobotoTextViewStyle</item> 
     <item name="android:buttonStyle">@style/RobotoButtonStyle</item> 
    </style> 

    <style name="RobotoTextViewStyle" parent="android:Widget.TextView"> 
     <item name="android:fontFamily">sans-serif-light</item> 
    </style> 

    <style name="RobotoButtonStyle" parent="android:Widget.Holo.Button"> 
     <item name="android:fontFamily">sans-serif-light</item> 
    </style> 

而在清單文件我有

android:theme="@style/AppTheme" 

但仍當我在Android上運行的程序4.4.2注3 ,漫畫sans字體仍然使用,是否有任何理由?

回答

1

剛剛嘗試創建像

public class TypefaceClass { 


    public static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>(); 

    public static Typeface get(Context context, String assetPath) { 
     synchronized (cache) { 
      if (!cache.containsKey(assetPath)) { 
       try { 
        Typeface t = Typeface.createFromAsset(context.getAssets(), 
          assetPath); 
        cache.put(assetPath, t); 
       } catch (Exception e) {      
        return null; 
       } 
      } 
      return cache.get(assetPath); 
     } 
    } 
} 

一個字體類,比創建您的資產字體文件夾,並把你的字體文件夾中。然後通過使用上面的字體類來設置字體。

textView.setTypeface(TypefaceClass.get(context, 
       "font/your_font.ttf")); 

如果它不起作用請嘗試使用.otf文件而不是.ttf文件。它一定會解決你的問題。上述4.4中的.ttf文件存在一些問題。所以嘗試獲取字體的.otf文件。

+0

我以前的方法有什麼問題?我也從其中的一個線程得到了它 – cherhan 2014-09-13 04:39:53