简体   繁体   English

Android:对一个活动的片段应用不同的主题

[英]Android: Apply different themes to fragments of one activity

What I want to do:我想做的事:

I want each Fragment of my MainActivity to use a different theme, so that the ActionBar has different background-colors, depending on the visible Fragment.我希望 MainActivity 的每个 Fragment 使用不同的主题,以便 ActionBar 具有不同的背景颜色,具体取决于可见的 Fragment。

The Situation:情况:

I created a MainActivity which uses the Tabs + Swipe Navigation.我创建了一个使用 Tabs + Swipe Navigation 的 MainActivity。 I Added 7 Tabs (=7 Fragments).我添加了 7 个标签(= 7 个片段)。 I created one Theme which should be applied only to the first Fragment (fragment_main_1).我创建了一个应该仅应用于第一个 Fragment (fragment_main_1) 的主题。

Here the Theme:这里的主题:

<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>

After creating 6 more Themes it should be possible to swipe through the Tabs while the ActionBar changes its background-color automatically.创建 6 个其他主题后,应该可以在 ActionBar 自动更改其背景颜色时滑动选项卡。

What didn't work:什么不起作用:

Adding those lines (which I found here on stackoverflow) to the Fragment1.java:将这些行(我在 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);

I hope you can help me:) Thank you.我希望你能帮助我:)谢谢。

Many fragments (such as PreferenceFragment ) read the styled attributes directly from the Context returned by Fragment.getContext() method, so you might need to override that too:许多片段(例如PreferenceFragment )直接从Fragment.getContext()方法返回的Context读取样式属性,因此您可能也需要覆盖它:

private var themedContext: Context? = null

override fun onAttach(context: Context) {
    super.onAttach(context).also {
        themedContext = ContextThemeWrapper(context, R.style.ThemeForThisFragment)
        // if you want to apply a theme overlay:
        // themedContext.theme.applyStyle(R.style.MyThemeOverlay, true)
    }
}

override fun onDetach() {
    super.onDetach()
    themedContext = null
}

override fun getContext(): Context? {
    return themedContext ?: super.getContext()
}

Try LayoutInflater localInflater = inflater.from(contextThemeWrapper);尝试LayoutInflater localInflater = inflater.from(contextThemeWrapper); instead.反而。

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

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