机器人:如何获取当前主题的资源ID? [英] Android: How to obtain the resource ID of the current theme?
问题描述
在Android中,你可以得到一个活动从 getTheme()
Resource.Theme 对象的当前主题>。此外,您还可以设置主题,通过其他主题的资源ID一个不同的,如 setTheme(R.style.Theme_MyTheme)
。
In Android, you can get the current theme of an activity as a Resource.Theme
object from getTheme()
. Also, you can set the theme to a different one via that other theme's resource id, as in setTheme(R.style.Theme_MyTheme)
.
但是,如何才能知道它是否是值得的 - 当前主题是否已经是我想要设置的呢?我在寻找类似 getTheme()getResourceId()
,才能写出这样的:
But how do I find out whether it's worth it -- whether the current theme is already the one that I would want to set? I am looking for something like getTheme().getResourceId()
, in order to write something like:
protected void onResume() {
int newThemeId = loadNewTheme();
if (newThemeId != getTheme().getResourceId()) { // !!!! How to do this?
setTheme(newThemeId);
// and rebuild the gui, which is expensive
}
}
任何想法?
推荐答案
确定这里有一个一块拼图:我们可以得到默认主题,如在AndroidManifest.xml中设置为 context.getApplicationInfo()。主题
为主题定在应用层面上,并从活动中,如 getPackageManager()。getActivityInfo(getComponentName(),0)。主题
该活动。
OK here's one puzzle piece: we can get the default theme, as set in the AndroidManifest.xml, as context.getApplicationInfo().theme
for the theme set at application level, and from within an Activity, as getPackageManager().getActivityInfo(getComponentName(), 0).theme
for that activity.
我想这给了我们一个起点,我们自己做包装,定制 getTheme()
和 setTheme()
。
I guess that gives us a starting point to do our own wrapper for a custom getTheme()
and setTheme()
.
不过,感觉像工作的围绕的而不是与的的API。所以我会离开的问题公开,看看是否有人想出了一个更好的主意。
Still that feels like working around rather than with the API. So I'll leave the question open to see if someone comes up with a better idea.
这篇关于机器人:如何获取当前主题的资源ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!