简体   繁体   English

在Android中以编程方式设置主题

[英]Setting a theme Programmatically in Android

I've seen this done before in Android so I know it's possible. 我之前已经在Android中看到了这一点,所以我知道这是可能的。 I want to add a preference that changes the app theme using a listPreference. 我想添加一个使用listPreference更改应用程序主题的首选项。 I've been fiddling around with some XML code but I'm stuck on the Java part. 我一直在摆弄一些XML代码,但是我坚持使用Java。

Here is the listPreference in my settings.xml file: 这是我的settings.xml文件中的listPreference:

<ListPreference
        android:entries="@array/Theme"
        android:entryValues="@array/themeAlias"
        android:key="theme_chooser"
        android:title="Theme"
        android:summary="Change the theme of the main activity." />

And here is my string arrays in my array.xml file: 这是我的array.xml文件中的字符串数组:

<string-array name="Theme">
        <item>Default Theme</item>
        <item>Holo Light</item>
</string-array>
<string-array name="themeAlias">
        <item>"default"</item>
        <item>"light"</item>
</string-array>

Now here is some psuedocode that I don't know how to write into actual code: 现在这是一些伪代码,我不知道该如何写到实际代码中:

Find the listPreference
   if preference is equal to default
       set the main activity's theme to custom a theme
   else
       set the theme to a standard light theme

I have done about 20 Google searches for this and all I get is a bunch of theming apps and blog posts about how to theme your android phone like a boss...I want to have users theme my app like a boss! 我已经完成了约20个Google搜索,而我得到的只是一堆主题应用程序和博客文章,内容涉及如何像老板一样主题化您的Android手机...我希望让用户像我的老板一样来主题化我的应用程序!

Thanks in advance, 提前致谢,

Andrew 安德鲁

请查看此答案 ,该答案建议为基本活动设置主题,该主题将由您的所有应用活动扩展。

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

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