【问题标题】:Change PreferenceFragment font via asset fonts通过资产字体更改 PreferenceFragment 字体
【发布时间】:2013-12-11 10:47:52
【问题描述】:

为了在 PreferenceFragment 中为每个偏好设置自定义字体,我必须为 每个 偏好类型(CustomSwitchPreferenceCustomEditTextPreferenceCustomListPreference,... .) 并在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 包括对话框的所有段的字体?

【问题讨论】:

    标签: android android-fragments customization android-preferences


    【解决方案1】:

    在您的styles.xml 中,添加以下样式:

    <style name="PreferenceTheme" parent="@style/AppTheme">
        <item name="android:fontFamily">@font/lato</item>
        <item name="fontFamily">@font/lato</item>
    </style>
    

    然后,在 SettingsFragment.java 中,覆盖 onCreateView():

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        container.getContext().setTheme(R.style.PreferenceTheme);
        return view;
    }
    

    这通常可以解决问题。如果没有,将样式添加到 AndroidManifest.xml:

    <application
        ...>
        <activity.../>
        <activity
            android:name=".SettingsActivity"
            android:theme="R.style.PreferenceTheme"/>
        ...
    </application>
    

    我得到了一个简洁的结果: Preferences with custom font - "Lato"

    【讨论】:

      猜你喜欢
      • 2012-07-06
      • 1970-01-01
      • 2015-06-26
      • 1970-01-01
      • 2019-07-16
      • 1970-01-01
      • 2011-07-31
      • 2017-10-25
      • 2017-11-07
      相关资源
      最近更新 更多