我想要做什么:
我希望我的MainActivity中的每个Fragment使用不同的主题,这样ActionBar的背景颜色根据当前可见的Fragment不同而变化。
现状:
我创建了一个使用选项卡和滑动导航的MainActivity。我添加了7个选项卡(= 7个Fragments)。我创建了一个主题,仅应用于第一个Fragment(fragment_main_1)。
以下是该主题:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Blue" parent="android:Theme.Holo.Light">
<item name="android:actionBarStyle">@style/Blue.ActionBarStyle</item>
</style>
<style name="Blue.ActionBarStyle" parent="android:Widget.Holo.Light.ActionBar">
<item name="android:titleTextStyle">@style/Blue.ActionBar.TitleTextStyle</item>
<item name="android:background">#33B5E5</item>
</style>
<style name="Blue.ActionBar.TitleTextStyle" parent="android:TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#FFFFFF</item>
</style>
</resources>
创建了6个主题后,就可以通过滑动选项卡来自动更改操作栏的背景色。
没有成功的方法:
将这些代码行(我在stackoverflow上找到的)添加到Fragment1.java中:
// create ContextThemeWrapper from the original Activity Context with the custom theme
final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.Blue);
// clone the inflater using the ContextThemeWrapper
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
// inflate the layout using the cloned inflater, not default inflater
return localInflater.inflate(R.layout.fragment_main_1,container, false);
我希望你能帮助我:)谢谢。