Jon*_*nas 21 android android-layout
我想做的事:
我希望我的MainActivity的每个片段使用不同的主题,以便ActionBar具有不同的背景颜色,具体取决于可见的片段.
情况:
我创建了一个使用Tabs + Swipe Navigation的MainActivity.我添加了7个标签(= 7个片段).我创建了一个主题,应该只应用于第一个片段(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>
Run Code Online (Sandbox Code Playgroud)
在创建了6个主题之后,应该可以在ActionBar自动更改其背景颜色的同时滑动选项卡.
什么行不通:
将这些行(我在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);
Run Code Online (Sandbox Code Playgroud)
我希望你能帮帮我:)谢谢.
尝试LayoutInflater localInflater = inflater.from(contextThemeWrapper);一下。
| 归档时间: |
|
| 查看次数: |
11175 次 |
| 最近记录: |