簡體   English   中英

通過設置菜單更改應用字體

[英]Change font for application through a setting menu

我正在嘗試實現一項功能,用戶可以激活“閱讀障礙”模式,該模式將整個應用程序的字體更改為以res.tff文件格式保存的字體。

從閱讀的內容來看,這似乎是一項非常艱巨的任務。 我知道可以為整個應用程序使用自定義字體( 從此處開始 ),但是據我對本示例的了解,它不適合通過onClick方法切換字體。

有沒有一種方法可以通過按鈕觸發?

您可以使用自己的類擴展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中設置新字體。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM