프로젝트의 모든 곳에서 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 새 글꼴을 설정하십시오.
Button에도이 작업을 수행해야합니까? – MERCURIUS
TextView를 사용하여 적절한 배경을 가진 Button을 구현할 수 있으므로 "MyTextView"도 같은 방식으로 작동합니다. 그러나 당신이 어쨌든 Button을 사용하기를 원한다면, 그렇습니다.이 버튼도 확장해야합니다. –