Change PreferenceFragment font via asset fonts

后端 未结 1 1456
不思量自难忘°
不思量自难忘° 2021-01-12 09:11

In order to have custom fonts for each Preference in PreferenceFragment, I had to write a new customized class for each preference type (CustomSwitchP

相关标签:
1条回答
  • 2021-01-12 09:29

    In your styles.xml, add the following style:

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

    Then, in SettingsFragment.java, override the 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;
    }
    

    This usually does the trick. If not, add the style to AndroidManifest.xml:

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

    I'm getting a neat result: Preferences with custom font - "Lato"

    0 讨论(0)
提交回复
热议问题