2013-03-27 13 views
18

の断片に異なるテーマを適用します。アンドロイド:私は何をしたいか一つの活性

を私はアクションバーがに応じて、異なる背景色を持っているように、私のMainActivityの各フラグメントは、別のテーマを使用したいです可視フラグメント。

状況:

私はタブ+スワイプナビゲーションを使用していますMainActivityを作成しました。私は7つのタブ(= 7つのフラグメント)を追加しました。最初のフラグメント(fragment_main_1)にのみ適用する必要があるテーマを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つのより多くのテーマを作成した後、アクションバーは自動的に背景色を変更しながら、タブをスワイプすることが可能であるべきです。私はあなたが私を助けることができると思います

// 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); 

:)ありがとう:

はFragment1.javaに(私はstackoverflowの上ここにあります)これらの行を追加:どのような動作しませんでした

+1

@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); } 

あなたは今までに解決策を見つけますか? – jcaruso

+0

いいえ、私はそのプロジェクトを断念しました:( – Jonas

+0

誰でもこの質問への回答が見つかりましたか? –

答えて

2

代わりにLayoutInflater localInflater = inflater.from(contextThemeWrapper);を試してください。

+1

あなたの素早い返信をありがとうが、それは助けてくれませんでした:( – Jonas

3

設定マニフェストのテーマは、通常、アクティビティに使用されます。

あなたは、フラグメントのためのテーマを設定する断片のonCreateView()に次のコードを追加したい場合:[Source]

+15

あなたは少なくともこのコンテンツの元の著者に言及することができます:http://stackoverflow.com/a/15496425/4744263のいずれかのCtrl + C Ctrl-Vの前に入力します。 –

関連する問題