6

내 응용 프로그램에서 sans-serif light를 기본 글꼴로 설정하고 싶습니다. Android Lollipop 기기에서 작업하고 있습니다. 내 장치에서 응용 프로그램을 실행하면, 산세 리프 빛은 모든보기에Android Lollipop에서 Fontfamily가 작동하지 않습니다.

<resources> 

    <style name="AppBaseTheme" parent="android:Theme.Material.Light.DarkActionBar"> 

    </style> 

    <!-- 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.Button"> 
     <item name="android:fontFamily">sans-serif-light</item> 
    </style> 

</resources> 

적용되지 않은 : 그래서,이 내 styles.xml입니다. 예를 들어 ActivityMain.java의 TextViews는 원하는 글꼴로 표시되지만 SecondActivity.java와 같은 다른 활동에서는 모든 TextView가 정상적으로 나타납니다. Android 4.1이 설치된 기기에서 앱을 실행하면 모든보기에서 작동합니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

답변

0

재질 디자인 테마를 사용하는 경우 사전에 :) 감사 당신에게 중요하지 않습니다, 당신은이를 사용할 수 있습니다

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar"> 
<item name="android:typeface">sans-serif-light</item> 
</style> 

재질 테마를 사용하는 응용 프로그램에 대한 중요한 경우, 다음과 같은 기술을 사용할 수 있습니다 :

import java.lang.reflect.Field; 
import android.content.Context; 
import android.graphics.Typeface; 

public final class FontsOverride { 

public static void setDefaultFont(Context context, 
     String staticTypefaceFieldName, String fontAssetName) { 
    final Typeface regular = Typeface.createFromAsset(context.getAssets(), 
      fontAssetName); 
    replaceFont(staticTypefaceFieldName, regular); 
} 

protected static void replaceFont(String staticTypefaceFieldName, 
     final Typeface newTypeface) { 
    try { 
     final Field staticField = Typeface.class 
       .getDeclaredField(staticTypefaceFieldName); 
     staticField.setAccessible(true); 
     staticField.set(null, newTypeface); 
    } catch (NoSuchFieldException e) { 
     e.printStackTrace(); 
    } catch (IllegalAccessException e) { 
     e.printStackTrace(); 
    } 
} 
} 

이제 응용 프로그램 클래스의 기본 글꼴을 오버로드

public final class Application extends android.app.Application { 
@Override 
public void onCreate() { 
    super.onCreate(); 

    FontsOverride.setDefaultFont(this, "SANS_SERIF", "sans_serif_light.ttf"); 
} 
} 
관련 문제