2013-03-27 3 views
18

То, что я хочу сделать:Android: Применять различные темы для фрагментов одного вида деятельности

Я хочу, чтобы каждый фрагмент моего MainActivity использовать другую тему, так что ActionBar имеет различный фоновый-цвет, в зависимости от видимый фрагмент.

Ситуация:

Я создал MainActivity, который использует Tabs + Размах навигации. Я добавил 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> 

После создания еще 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); 

Я надеюсь, что вы можете помочь мне :) Спасибо ,

+1

ли вы когда-нибудь найти решение? – jcaruso

+0

Нет, я отказался от этого проекта :( – Jonas

+0

Кто-нибудь нашел ответ на этот вопрос? –

ответ

2

Попробуйте LayoutInflater localInflater = inflater.from(contextThemeWrapper); вместо этого.

+1

Спасибо за ваш быстрый ответ, но это не помогло :( – Jonas

3

Настройка темы в манифесте обычно используется для Activity.

Если вы хотите установить тему для фрагмента, добавьте следующий код в onCreateView() фрагмента:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

// create ContextThemeWrapper from the original Activity Context with the custom theme 
final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.yourCustomTheme); 

// 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.yourLayout, container, false); 
} 

[Source]

+15

Вы могли хотя бы упомянуть оригинального автора этого контента: http://stackoverflow.com/a/15496425/4744263 перед любым Ctrl-C Ctrl-V. –

Смежные вопросы