2017-11-27 12 views
0

사용자가 '실독증'모드를 활성화 할 수있는 기능을 구현하려고합니다. 전체 응용 프로그램의 글꼴을 .tff 파일로 res에 저장된 글꼴로 변경합니다.설정 메뉴를 통해 응용 프로그램의 글꼴 변경

주위를 둘러 보니 매우 어려운 작업 인 것 같습니다. 전체 응용 프로그램 (from here)에 사용자 지정 글꼴을 사용할 수 있다는 것을 이해했지만이 예제를 이해할 때부터는 onClick 메서드를 통해 글꼴을 전환하는 데 적합하지 않습니다.

버튼으로 트리거하는 방법이 있습니까?

답변

0

프로젝트의 모든 곳에서 TextView를 자신의 클래스로 확장 할 수 있습니다.

public class MyTextView extends TextView { 

    public MyTextView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(context); 
    } 

    public MyTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(context); 
    } 

    public MyTextView(Context context) { 
     super(context); 
     init(context); 
    } 

    private void init(Context context) { 
     Typeface tf = 
     Typeface.createFromAsset(context.getAssets(), getFontFromPreferences(context)); 
     setTypeface(tf); 
    } 

    private String getFontFromPreferences(Context context) { 
     SharedPreferences preferences = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE); 
     String fontString = preferences.getString("my_font_pref_key", "Default.tff"); 
     return fontString; 
    } 
} 

설정 버튼에 SharedPreferences 새 글꼴을 설정하십시오.

+0

Button에도이 작업을 수행해야합니까? – MERCURIUS

+0

TextView를 사용하여 적절한 배경을 가진 Button을 구현할 수 있으므로 "MyTextView"도 같은 방식으로 작동합니다. 그러나 당신이 어쨌든 Button을 사용하기를 원한다면, 그렇습니다.이 버튼도 확장해야합니다. –