2013-03-27 13 views
18

Co chcę zrobić:Android: Zastosuj różne tematy do fragmentów jednej działalności

chcę każdy fragment mojego główną działalność, aby wykorzystać inny motyw, tak że ActionBar ma różne tła kolory, w zależności od widoczny fragment.

Sytuacja:

stworzyłem główną działalność, która używa Tabs + Swipe Nawigacja. Dodałem 7 kart (= 7 fragmentów). Stworzyłem jeden motyw, który powinien być zastosowany tylko do pierwszego Fragmentu (fragment_main_1).

Tutaj Temat:

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

Po utworzeniu 6 więcej tematów powinny być możliwe do machnięcia poprzez zaczepy natomiast ActionBar zmienia background-color automatycznie.

Co nie działa:

Dodając te linie (które znalazłem tutaj na stackoverflow) do 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); 

Mam nadzieję, że może mi pomóc :) Dziękuję .

+1

Czy kiedykolwiek znaleźć rozwiązanie? – jcaruso

+0

Nie, zrezygnowałem z tego projektu :( – Jonas

+0

ktoś znalazł odpowiedź na to pytanie? –

Odpowiedz

2

Zamiast tego spróbuj .

+1

Dziękuję za szybką odpowiedź, ale to nie pomogło :( – Jonas

3

Motyw ustawiania w manifeście jest zwykle używany do działania.

Jeśli chcesz ustawić motyw dla fragmentu dodać kolejny kod w onCreateView() fragmentu:

@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

Możesz przynajmniej wspomnieć o oryginalnym autorze tej treści: http://stackoverflow.com/a/15496425/4744263 przed dowolnym Ctrl-C Ctrl-V. –

Powiązane problemy