android – 通过资产字体更改PreferenceFragment字体

为了在PreferenceFragment中为每个Preference设置自定义字体,我必须为每个首选项类型(CustomSwitchPreference,CustomEditTextPreference,CustomListPreference,….)编写一个新的自定义类,并在onBindView方法中设置其字体.

它有效,但这是最好的解决方案吗?不短吗?

@Override
public void onBindView(View view){
    super.onBindView(view);
    TextView title = (TextView) view.findViewById(android.R.id.title);
    TextView summary = (TextView) view.findViewById(android.R.id.summary);
    Utils.setFont(context,title,customfont);
    Utils.setFont(context,summary,customfont);
}

public class Utils{
    public static boolean setFont(Context context,TextView tv,String fontAssetName) {
        Typeface font = Typeface.createFromAsset(context.getResources().getAssets(),fontAssetName);
        if (font != null) {
            tv.setTypeface(font);
            return true;
        }
        return false;
    }
}

有没有办法更改PreferenceFragment的所有片段的字体,包括对话框?

解决方法

你试过这个吗?

Custom fonts in Android the easy way …

这听起来很有希望.

相关文章

文章浏览阅读8.8k次,点赞9次,收藏20次。本文操作环境:win1...
文章浏览阅读1.2w次,点赞15次,收藏69次。实现目的:由main...
文章浏览阅读3.8w次。前言:最近在找Android上的全局代理软件...
文章浏览阅读2.5w次,点赞17次,收藏6次。创建项目后,运行项...
文章浏览阅读8.9w次,点赞4次,收藏43次。前言:在Android上...
文章浏览阅读1.1w次,点赞4次,收藏17次。Android Studio提供...