2016-07-29 18 views
4

SettingsActivityを作成して、ユーザーにアプリの外観をパーソナライズできるようにしたいと考えています。 このアクティビティでは、アプリを「ライトテーマ」(つまり、白い背景に黒いテキストがあることを意味します)または「ダークテーマ」(明るいテーマの反対の色つかいます。プロジェクトを更新してマルチテーマアプリを作成する方法

どうすればできますか?

テーマごとに異なるレイアウトをXMLで作成しようと考えていました。以下

EDITS

画像はSettingsActivityの例であり、私は、全体のアプリではなく、単一のアクティビティのためappearenceを変更したいと思います。

enter image description here

enter image description here

enter image description here

enter image description here

答えて

1

これは私のアプリのために行った方法です。私のアプローチが助けになると確信しています。

は次のように のstyles.xmlにあなたの光と闇のテーマを設定します。

 <!-- Use this theme in Manifest by default --> 
    <style name="MyLightTheme" parent="Base.AppTheme.Light"></style> 
      
    <!-- Base light theme containing all styles --> 
    <style name="Base.AppTheme.Light" parent="Theme.AppCompat.Light.NoActionBar"> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
     ... Other styles 
    </style> 

    <!-- Use this to switch to Dark theme --> 
    <style name="MyDarkTheme" parent="Base.AppTheme.Dark"></style> 

    <!-- Base dark theme containing all styles --> 
    <style name="Base.AppTheme.Dark" parent="Theme.AppCompat"> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
     ... Other styles 
    </style> 

お好み経由テーマの変更を制御しているので、あなたのPreferenceFragmentに設定変更リスナーを登録します。

PreferenceManager.getDefaultSharedPreferences(getActivity()).registerOnSharedPreferenceChangeListener(this); 

onSharedPreferenceChanged ()を実装します。

@Override 
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { 

     if (key.equals(getString(R.string.pref_key_nighttheme))) { 

      if (sharedPreferences.getBoolean(getString(R.string.pref_key_nighttheme), false)) { 
       // Night theme enabled 

       getActivity().setTheme(R.style.MyDarkTheme); 
         getActivity().getApplication().setTheme(R.style.MyDarkTheme); 
       darkTheme = true; 

      } else { 
       getActivity().setTheme(R.style.MyLightTheme); 
       getActivity().getApplication().setTheme(R.style.MyLightTheme); 
       darkTheme = false; 
      } 

      getActivity().recreate(); // This is important. It allows the theme change to take effect. 
     } 
    } 

戻るナビゲーションMainActivityにつながる場合は、あなたのMainActivityでonResume()を再作成してください。


また、あなたはがのonCreate()で呼び出されスーパー()の前に、すべての活動で、現在のテーマを確認する必要があります。

isThemeDark = setDarkTheme(this); 

setDarkTheme()SharedPreferenceを介して電流のテーマをチェックし、私が作成したヘルパーです。テーマの変更が必要かどうかをチェックします。ここで

public static boolean setDarkTheme(Context context) { 
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 

    boolean isDarkTheme = prefs.getBoolean(context.getString(R.string.pref_key_nighttheme), false); 
    context.setTheme(SettingsActivity.darkTheme ? R.style.MyDarkTheme : R.style.MyLightTheme); 

    return isDarkTheme; 
} 

は、ナイトモードがNewslet私のアプリで動作する方法は次のとおりです。

enter image description here

UPDATE: アンドロイドは現在、正式にAPPCOMPAT 白黒モードテーマ経由ナイトモードをサポートしています。 同じものにtutorialがあります。

2

独自のテーマを作成することができ、ユーザーがそれらをあなたの活動にこのコードを追加変更したいとき、あなたはいずれかを選択することができますあなただけのHoloだけではありませんテーマ。

getApplication()。setTheme(Theme.Holo)

+0

の表情を持っていますが、どのように私はすべてのレイアウトを変更することができます_SettingsActivity_の_CheckBox_から、単純に1つのスタイルでアプリの私が持っているすべてのビューは独自の "パーソナライゼーション"を持っています – Pier

+0

独自のスタイルを作成し、マニフェストファイルからすべてのアクティビティのスタイルを変更することができます。 – RadiBarq

+0

私が求めているのは明らかではないかもしれません...マニフェストから1つのアクティビティにつき1つのスタイルを変更する必要はありません。 私は上記の編集の画像で見ることができるように、他のパーソナライズの中で、アプリ全体のマルチプルテーマ(緑、青、黒、白、赤)を切り替えることができる、SettingsActivityが必要です。 レイアウトにはそれぞれ異なるビューがありますので、私の意見では、スタイルの変更をすべて行うことは難しいです – Pier

2

たぶん、この答えは、この問題 https://stackoverflow.com/a/4673209/4858673

public class BaseActivity extends Activity { 

    protected void onCreate(Bundle state) { 
     super.onCreate(state); 
     String theme = PreferenceManager.getDefaultSharedPreferences(this).getString("theme", "black"); 
     setTheme(getTheme(theme); 
    } 

    private int getTheme(String theme) { 
     if (theme.equals("black") return R.style.ThemeBlack; 
     if (theme.equals("white") return R.style.ThemeWhite; 
     ... 
     return android.R.style.Theme; // stub 
    } 
} 

であなたを助け、このBaseActivityからすべてのあなたの活動を延長することができます。 PreferenceActivityまたはPreferenceFragmentを使用する場合は、このコードを実装に入れて、アクティビティ(AppCompatActivity)またはPreferenceActivityを1つだけ拡張できます。

P.S. R.style.ThemeBlackはstyles.xmlのテーマ設定です

<style name="ThemeBlack" parent="android:Theme.Holo"> 
</style> 
0
  1. まず、明確に定義されたテーマをxml-stylesで作成する必要があります。

    <style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar"> 
        <!-- Customize your theme here. --> 
        <item name="colorPrimary">@color/primaryColorAmber</item> 
        <item name="colorPrimaryDark">@color/primaryDarkColorAmber</item> 
        <item name="colorAccent">@color/secondaryColorAmber</item> 
    </style> 
    
    <style name="AppTheme.NoActionBar"> 
        <item name="windowActionBar">false</item> 
        <item name="windowNoTitle">true</item> 
    </style> 
    
    <style name="AppTheme.RED" parent="AppTheme.NoActionBar"> 
        <!-- Customize your theme here. --> 
        <item name="colorPrimary">@color/primaryColorRed</item> 
        <item name="colorPrimaryDark">@color/primaryDarkColorRed</item> 
        <item name="colorAccent">@color/secondaryColorRed</item> 
    </style> 
    
    <style name="AppTheme.PINK" parent="AppTheme.NoActionBar"> 
        <!-- Customize your theme here. --> 
        <item name="colorPrimary">@color/primaryColorPink</item> 
        <item name="colorPrimaryDark">@color/primaryDarkColorPink</item> 
        <item name="colorAccent">@color/secondaryColorPink</item> 
    </style> 
    
  2. は、お使いのベースアクティビティのonCreate()メソッド内のコードだけsetContentViewの前に()以下のランタイム・使用時にテーマを変更します。

    //テーマを変更するには、テーマIDだけを入れてください。

    int theme = getThemeFromPreferences(); // R.style.AppTheme_RED 
    setTheme(theme); 
    
  3. (テーマを変えているところから)あなたの設定/プリファレンス活動のテーマを変更するには、その活動の次のメソッドを呼び出すことでその活動を再作成する必要があります。

    //好みの詳細およびサンプルコードについては

    saveThemeInPreferences(R.style.AppTheme_RED); 
    //recreate this activity to see changes 
    SettingActivity.this.recreate(); 
    

にあなたのテーマIDを保存... は Android multi theme implementation

関連する問題