简体   繁体   English

以编程方式修改Android主题

[英]Modify Android Theme Programmatically

A similar question to this has been asked many times; 有人问了很多次类似的问题; however, an answer has not been given that addresses my situation. 然而,没有给出解决我的情况的答案。 I need to dynamically change an application's theme based on color values that are being returned from an API call. 我需要根据API调用返回的颜色值动态更改应用程序的主题。 I then need to change the theme colors of the app based on the values returned. 然后我需要根据返回的值更改应用程序的主题颜色。 Therefore, I have no way of saving the colors in a style XML file. 因此,我无法在样式XML文件中保存颜色。 Can this be done? 可以这样做吗?

I have a base activity, and my plan is to set the app theme from there for all the activities. 我有一个基础活动,我的计划是从那里为所有活动设置应用主题。

Unfortunately, I did not find an easy way to do this. 不幸的是,我没有找到一个简单的方法来做到这一点。 I created a ThemeColor class which holds all the colors returned by the API. 我创建了一个ThemeColor类,它包含API返回的所有颜色。 Then for each activity I have to go through every widget and style it. 然后,对于每个活动,我必须浏览每个小部件并设置它的样式。

Example: 例:

getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor(themeColor.getActionBarColor)));
this.getWindow().getDecorView().setBackgroundColor(Color.parseColor(themeColor.getBackgroundColor()));

etc 等等

I was not able to locate a simple way to resolve this issue either. 我也无法找到解决此问题的简单方法。 By creating a ThemeColor class that holds all the colors returned from the API. 通过创建一个ThemeColor类来保存从API返回的所有颜色。 Next for each activity I needed to address each widget separately as well as style it. 接下来我需要分别处理每个小部件并为其设置样式。

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

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