2017-12-07 3 views
0

이전 프로젝트에서 전체 응용 프로그램의 글꼴을 설정하기 위해 Calligraphy 라이브러리를 사용했습니다. 그러나 글꼴 파일을 애셋에 저장해야 APK 크기가 커집니다. 이제 전체 응용 프로그램의 기본값으로 downloadable font을 설정할 수 있는지 궁금합니다.전체 응용 프로그램에 다운로드 할 수있는 글꼴을 기본 글꼴로 설정하는 방법은 무엇입니까?

하나의 TextView에 대해서만 다운로드 가능한 글꼴을 설정할 수 있습니다.

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="@dimen/text_margin" 
    android:fontFamily="@font/lato" 
    android:text="@string/large_text" /> 

예, 나는 MyTextView 클래스를 생성하고 프로그램 다운로드 글꼴을 설정할 수 있습니다 알고 있습니다. 그러나 나는 텍스트가 EditText, Spinner 항목, Toast에있을 수 있기 때문에 좋은 생각이라고 생각하지 않습니다.

내 질문은 기본 글꼴로 전체 응용 프로그램에 대한 다운로드 가능한 글꼴을 설정하는 방법입니까?

답변

0

A library to help with custom fonts and text sizes

이 라이브러리의 목표는 앱이 자신의 스타일로 (예를 들어 라토, Roboto로 등) 여러 FontFamilies을 지원 할 수 있도록 (예 : 일반, 굵게, 기울임 꼴)에서 쉽게 구성 할 수있는 방법입니다 .

+0

하지만이 라이브러리는 전체 응용 프로그램에서 작동하지 않습니다. –

0

앱의 모든 곳에서 XML로 글꼴 세트를 적용하려면 themes.xml에 테마를 만들고 android:fontFamily을 설정하십시오.

<style name="ApplicationTheme"> 
    <item name="android:fontFamily">@font/lato</item> 
</style> 

설정 매니페스트

<application 
    android:name=".App" 
    android:icon="@mipmap/ic_launcher" 
    android:theme="@style/ApplicationTheme"> 
... 
</application> 

하고 당신이

같은 시스템 스타일에서 상속 스타일을 사용하지 않는 한 이
<style name="CustomButton" parent="Base.Widget.AppCompat.Button.Borderless"> 

글꼴이 적용 얻을 것이다 귀하의 응용 프로그램에이 테마 모든 텍스트 뷰, 편집 텍스트, 스피너, 토스트 등

0
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, "DEFAULT", "MyFontAsset.ttf"); 
     FontsOverride.setDefaultFont(this, "MONOSPACE", "MyFontAsset2.ttf"); 
     FontsOverride.setDefaultFont(this, "SERIF", "MyFontAsset3.ttf"); 
     FontsOverride.setDefaultFont(this, "SANS_SERIF", "MyFontAsset4.ttf"); 
    } 
} 
관련 문제