Android 为什么getTheme没有';我在申请方面做得不好
我意识到,对于Android 为什么getTheme没有';我在申请方面做得不好,android,Android,我意识到,对于Context.getTheme(),如果我们使用应用程序作为上下文 MyApplication.singletonInstance().getTheme().resolveAttribute(R.attr.actionBarDeleteIcon, typedValue, true); // typedValue.resourceId will be 0x0, which is invalid 但是,如果我使用活动作为上下文,则效果很好 MyFragment.this.getAc
Context.getTheme()
,如果我们使用应用程序
作为上下文
MyApplication.singletonInstance().getTheme().resolveAttribute(R.attr.actionBarDeleteIcon, typedValue, true);
// typedValue.resourceId will be 0x0, which is invalid
但是,如果我使用活动
作为上下文,则效果很好
MyFragment.this.getActivity().getTheme().resolveAttribute(R.attr.actionBarDeleteIcon, typedValue, true);
// typedValue.resourceId is valid
我想知道为什么我们不能通过应用程序解析属性
在清单中,我们可以在应用程序
级别找到特定的主题信息。所以,我认为从应用程序对象中获取主题是有意义的
<application
android:theme="..."
它不起作用,因为getApplicationContext()
返回的对象显然不是一个完整的上下文
对象,如下所述:
它不是一个完整的上下文
,支持活动
所做的一切。您尝试使用此上下文执行的各种操作都将失败,主要与GUI有关
一种可能的解决方案是在上下文中手动设置主题,如下所示:
getApplicationContext().getTheme().applyStyle(R.style.MyTheme, true);
但这种方法并没有得到Android开发团队的认可;正确的解决方案是使用Activity
处理与UI相关的事情,比如getTheme()