机器人:如何获取当前主题的资源ID? [英] Android: How to obtain the resource ID of the current theme?

查看:405
本文介绍了机器人:如何获取当前主题的资源ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆