问题
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
?
UPDATE
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.
回答1:
try this
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:textAppearanceListItemSecondary">@style/textAppearanceListItemSecondaryCustom</item>
</style>
<style name="textAppearanceListItemSecondaryCustom">
<item name="android:textSize">14sp</item>
<item name="android:typeface">monospace</item>
</style>
回答2:
Finally the way to change the font for summary of ListPreference
.
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"/>
Hope this help for other.
来源:https://stackoverflow.com/questions/42060639/change-font-typeface-of-summary-of-listpreference