Android 通过资产字体更改首选项片段字体
为了在PreferenceFragment中为每个首选项创建自定义字体,我必须为每个首选项类型编写一个新的自定义类(Android 通过资产字体更改首选项片段字体,android,android-fragments,customization,android-preferences,Android,Android Fragments,Customization,Android Preferences,为了在PreferenceFragment中为每个首选项创建自定义字体,我必须为每个首选项类型编写一个新的自定义类(CustomSwitchPreference,CustomEditTextPreference,CustomListPreference,…),并在onBindView方法中设置其字体 这是可行的,但这是最好的解决方案吗?没有矮一点的吗 @Override public void onBindView(View view){ super.onBindView(view);
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;
}
}
是否有任何方法可以更改
首选项片段
的所有片段(包括对话框)的字体?在styles.xml中,添加以下样式:
<style name="PreferenceTheme" parent="@style/AppTheme">
<item name="android:fontFamily">@font/lato</item>
<item name="fontFamily">@font/lato</item>
</style>
这通常能奏效。如果没有,请将样式添加到AndroidManifest.xml:
<application
...>
<activity.../>
<activity
android:name=".SettingsActivity"
android:theme="R.style.PreferenceTheme"/>
...
</application>
<application
...>
<activity.../>
<activity
android:name=".SettingsActivity"
android:theme="R.style.PreferenceTheme"/>
...
</application>