简体   繁体   English

Android:Support Fragments是否不应用活动主题?

[英]Android: Support Fragments don't apply activity theme?

At the moment, I'm stuck with a very annoying kind of "bug" I assume regarding all Pre-Lollipop Android versions. 目前,对于所有Pre-Lollipop Android版本,我都会遇到一个非常烦人的“错误”。 It appears that (support) fragments don't apply the activity's theme they're assigned to. (支持)片段似乎没有应用分配给他们的活动主题。 To make my explanations a bit easier, have the following demonstration: 为了使我的解释更容易一些,请进行以下演示:

My app runs with a turquoise theme at first. 首先,我的应用程序以绿松石为主题运行。 Let's say, the user decided to change the turquoise theme to a red theme. 假设用户决定将绿松石主题更改为红色主题。 He or she restarts the app and is greeted with the following: 他或她重新启动应用程序,并收到以下提示:

Android 4.4.2平板电脑:主题不适用于该片段

(screenshot taken on an Android 4.4.2 tablet) (屏幕截图是在Android 4.4.2平板电脑上拍摄的)

Terrible sight, isn't it? 可怕的景象,不是吗? However, if I run the same app in an emulator with Android L the whole theme problem doesn't even seem to exist. 但是,如果我在带有Android L的模拟器中运行相同的应用程序,那么整个主题问题似乎根本就不存在。

Android 5.0.1:主题适用于片段

There's especially one thing which seems odd about the tablet screenshot. 平板电脑屏幕截图尤其有一件奇怪的事。 The fragment itself doesn't apply the theme but child components inside the fragment which get added lateron (like the view with the exclamation mark which is hosted by a ViewPager) take and apply the theme as if nothing happened. 片段本身不应用主题,但是片段内部的子组件会在以后添加(例如,由ViewPager托管的带有感叹号的视图)采用并应用主题,就好像什么都没发生一样。

I'm not quite sure what the issue might be. 我不太确定可能是什么问题。 I've done everything as stated in every document available. 我已经完成了所有可用文档中所述的所有操作。 I set the theme before I call setContentView(resource) in the corresponding activity. 在相应活动中调用setContentView(resource)之前,我先设置了主题。 I tried to do the trick with ContextThemeWrapper but it was no use. 我试图用ContextThemeWrapper做到这ContextThemeWrapper但没有用。

Relevant code: 相关代码:

I tried to resolve this issue for days now and I still can't find out why this is not working. 我尝试解决此问题已有好几天,但仍然找不到原因。 I haven't found a suitable answer yet and would love some advise. 我还没有找到合适的答案,希望您能提出一些建议。

I've been the victim to my own stupidity. 我一直是自己愚蠢的受害者。 I had another close look on my project setup and found this: 我仔细查看了项目设置,发现了这一点:

在此处输入图片说明

My tablet takes its layout resources from the sw600dp-folder, but I forgot to change the corresponding layout to take attributes instead of hardcoded colors. 我的平板电脑从sw600dp文件夹中获取其布局资源,但是我忘记更改相应的布局以采用属性而不是硬编码的颜色。 I think I have to retire after making such a stupid mistake. 我想我必须在犯下如此愚蠢的错误后才退休。

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

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