更改`ListPreference`的摘要的字体字体 [英] Change font typeface of summary of `ListPreference`

查看:215
本文介绍了更改`ListPreference`的摘要的字体字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用此链接进行更改PreferenceScreen的所有首选项的字体. 除ListPreference的摘要外,其他地方均已更改. 谁能知道如何在ListPreference的摘要上应用自定义字体?

I tried with THIS LINK to change the typeface of all preferences of PreferenceScreen. It was changed everywhere except summary of ListPreference. Can anyone has an idea how to apply custom font on summary of ListPreference?

更新

我想将我的自定义字体应用于资源文件夹中,而不是内置字体.另外,android:textAppearanceListItemSecondary需要API 21,我的应用程序支持最低API15.

I want to apply my custom font exist on assets folder, not the in-build one. Also android:textAppearanceListItemSecondary required API 21, my app support minimum API 15.

推荐答案

最后,更改ListPreference摘要的字体的方法.

Finally the way to change the font for summary of ListPreference.

重写ListPreferenceonBindViewHolder方法以设置自定义字体.检查下面的自定义类.

Override the onBindViewHolder method of ListPreference to set your custom typeface. Check below custom class.

public class CustomListPreference extends ListPreference {
private String typefaceTitle, typefaceSummary;

public CustomListPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init(context, attrs);
}

public CustomListPreference(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs);
}

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

public CustomListPreference(Context context) {
    super(context);
}

@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
    super.onBindViewHolder(holder);
    if(!TextUtils.isEmpty(typefaceTitle)) {
        TextView titleView = (TextView) holder.findViewById(android.R.id.title);
        titleView.setTypeface(FontManager.getInstance().getFont(typefaceTitle));
    }
    if(!TextUtils.isEmpty(typefaceSummary)){
        final TextView summaryView = (TextView) holder.findViewById(
                android.R.id.summary);
        summaryView.setTypeface(FontManager.getInstance().getFont(typefaceSummary));
    }
}

private void init(Context context, AttributeSet attrs){
    TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.Preference);

    typefaceTitle = a.getString(R.styleable.Preference_typefaceTitle);
    typefaceSummary = a.getString(R.styleable.Preference_typefaceSummary);

    a.recycle();
}}

attrs.xml

<declare-styleable name="Preference">
    <attr name="typefaceTitle" format="string"></attr>
    <attr name="typefaceSummary" format="string"></attr>
</declare-styleable>

首选项"屏幕中的用法.

Usage in Preference screen.

<YOUR_CustomListPreference_PACKAGE_NAME.CustomListPreference
        android:defaultValue="X"
        android:dialogTitle="Dialog Title"
        android:entries="@array/list_item_title"
        android:entryValues="@array/list_item_value"
        android:key="pref_list"
        android:summary="%s"
        android:title="@string/Preference Title"
        app:typefaceTitle="YOUR FONT NAME"
        app:typefaceSummary="YOUR FONT NAME"/>

希望对其他人有帮助.

这篇关于更改`ListPreference`的摘要的字体字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆