简体   繁体   English

有没有一种方法可以在 Android 首选项中更改字体样式、颜色?

[英]Is there a method to change font style,color in Android Preferences?

I have created custom preferences into my app, which uses SharedPreferences to save data and I used PreferenceActivity.我在我的应用程序中创建了自定义首选项,它使用 SharedPreferences 来保存数据,我使用了 PreferenceActivity。

Now I need to modify its appearance.现在我需要修改它的外观。 Including font style, color, and font type.包括字体样式、颜色和字体类型。 If possible, add custom colors, background, add images, etc.如果可能,添加自定义颜色、背景、添加图像等。

Can I do that?我可以这样做吗? If so, how can I do it?如果是这样,我该怎么做?

Start off by creating a custom style whose parent is the app's theme.首先创建一个自定义样式,其父样式是应用程序的主题。 styles.xml样式文件

<style name="PreferenceTheme" parent="AppTheme">
    <item name="fontFamily">@font/whatever</item>
    ...
</style>

Then, in your Settings class,然后,在您的设置类中,

(if it extends PreferenceFragment, then do this in the onCreateView) (如果它扩展了 PreferenceFragment,则在 onCreateView 中执行此操作)

container.getContext().setTheme(R.style.PreferenceTheme);

(otherwise, if it extends PreferenceActivity, then do this in onCreate) (否则,如果它扩展了 PreferenceActivity,则在 onCreate 中执行此操作)

setTheme(R.style.PreferenceTheme);

First you create extend TextView class and create custom Typeface.首先创建扩展 TextView 类并创建自定义字体。 Then go to style folder and create new style and set color and size there.然后转到样式文件夹并创建新样式并在那里设置颜色和大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM