2014-09-13 22 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