2014-01-07 36 views
0

我正在嘗試爲應用程序添加主題支持。它正在工作,但我希望用戶在主題之間進行選擇。問題是,我做不到,我嘗試了很多東西。我使用ListPreference來定義供用戶選擇的數組列表。我無法將這些listpreference條目值與util關聯起來。 如果我在任何編號的Util中編輯「0」,主題可以工作,但當我更改這些條目值(從電話列表中)時,它不起作用。 下面是代碼使用listpreference爲應用程序添加主題支持

Settings.java

public class Settings extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     Util.setAppTheme(this); 
     super.onCreate(savedInstanceState); 

     // Display the fragment as the main content. 
     FragmentManager mFragmentManager = getFragmentManager(); 
     FragmentTransaction mFragmentTransaction = mFragmentManager 
       .beginTransaction(); 
     PrefsFragment mPrefsFragment = new PrefsFragment(); 
     mFragmentTransaction.replace(android.R.id.content, mPrefsFragment); 
     mFragmentTransaction.commit(); 


    } 

    public static class PrefsFragment extends PreferenceFragment { 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      // Load the preferences from an XML resource 
      addPreferencesFromResource(R.xml.preferences); 
     } 
     } 

    } 

Util.java

public class Util extends Activity { 

public static void setAppTheme(Activity a) { 

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(a); 
int mTheme = Integer.parseInt(sp.getString("theme", "0")); 
if(mTheme==0) 
{ 

    a.setTheme(R.style.Dark); 
} 
if(mTheme==1) 
{ 
    a.setTheme(R.style.Light); 
} 
if(mTheme==2) 
{ 
    a.setTheme(R.style.LimeLight); 
} 
if(mTheme==3) 
{ 
    a.setTheme(R.style.MojoLight); 
} 
if(mTheme==4) 
{ 
    a.setTheme(R.style.SanMarinoLight); 
} 
if(mTheme==5) 
{ 
    a.setTheme(R.style.LimeDark); 
} 
if(mTheme==6) 
{ 
    a.setTheme(R.style.MojoDark); 
} 
if(mTheme==7) 
{ 
    a.setTheme(R.style.SanMarinoDark); 
} 

} 
} 

的preferences.xml

<PreferenceCategory 
     android:title="@string/preference_category2_title"> 
     <ListPreference android:key="list2_preference" 
      android:title="@string/list2_title" 
      android:summary="@string/list2_summary" 
      android:entries="@array/list2_preferences" 
      android:entryValues="@array/list2_preferences_values" 
      android:dialogTitle="@string/list2_dialog_title"/> 
     <SwitchPreference android:key="switch1_preference" 
      android:title="@string/switch1_title" 
      android:switchTextOff="@string/switch1_textoff" 
      android:switchTextOn="@string/switch1_texton" 
      /> 
    </PreferenceCategory> 

styles.xml

<!-- Dark --> 
<style name="Dark" parent="android:Theme.Holo"> 

    <item name="android:windowBackground">@drawable/activity_background_dark</item> 
</style> 

<!-- Light --> 
<style name="Light" parent="android:Theme.Holo.Light"> 
    <item name="android:actionBarStyle">@style/ActionBar_Light</item> 
    <item name="android:windowBackground">@drawable/activity_background_light</item> 

</style> 

arrays.xml

<resources> 

<string-array name="list2_preferences"> 
    <item>Dark</item> 
    <item>Light</item> 
    <item>Light Lime</item> 
    <item>Light Mojo</item> 
    <item>Light San Marino</item> 
    <item>Dark Lime</item> 
    <item>Dark Mojo</item> 
    <item>Dark San Marino</item> 
</string-array> 

<string-array name="list2_preferences_values"> 
    <item>0</item> 
    <item>1</item> 
    <item>2</item> 
    <item>3</item> 
    <item>4</item> 
    <item>5</item> 
    <item>6</item> 
    <item>7</item> 

</string-array> 

我會感激如果有人能幫助我嗎?

回答

1

你在你的列表的onItemSelected方法的代碼是什麼?

另外,在初始化佈局後,您無法更改某個活動的主題。 在我目前的應用程序中,我也允許用戶更改主題。爲了達到這個目的,我只需再次通過一個參數(選定的主題)來啓動活動並完成當前的活動。 在活動onCreate()我然後檢查參數。如果它是-1(活動第一次開始)我得到默認主題。

private static final String INTENT_EXTRA = "theme"; 
private boolean onCreate; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    onCreate = true; 

    themeId = getIntent().getIntExtra(INTENT_EXTRA, -1); 
    if (themeId == -1) { 
     themeId = SharedPreferencesManagment 
       .getApplicationThemeResourceId(SharedPreferencesManagment 
         .getIntApplicationTheme(this)); 
    } else { 
     themeId = SharedPreferencesManagment 
       .getApplicationThemeResourceId(themeId); 
    } 
    setTheme(themeId); 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_options_theme); 

    initSpinner(); 
} 

在列表中的onItemSelected()我稱之爲是這樣的:

@Override 
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
    if (!onCreate) { 
     Intent intent = new Intent(this, OptionsThemeActivity.class); 
     intent.putExtra(INTENT_EXTRA, getSelectedTheme()); 
     startActivity(intent); 

     finish(); 
    } 
    onCreate = false; 
} 

還要注意的onCreate布爾爲onItemSelected() - 方法也被稱爲當inits微調活動。 (是防止無限循環的最簡單的解決方法)

ALSO: 爲什麼使用活動?如果您發佈的代碼是使用Util的所有內容,則在傳遞上下文時不需要擴展Activity。

+0

我是新來的應用程序編程。我學會了創建列表首選項,然後我學會了使用樣式創建主題。然後我不知道如何鏈接列表首選項與主題。我知道如何使用onclicklistener作爲按鈕,但idk關於listpreferences。你能告訴我在哪裏放置你已經給的onItemselected的代碼嗎?而上面的代碼,它放在哪裏?我不知道使用utils,在此之前我嘗試了很多東西,其中一個有util,所以我創建了util活動。它在一定程度上工作,但不鏈接到列表首選項我手動必須更改util內的值。 – Vishal

+0

任何人都可以告訴我,我如何鏈接我的styles.xml中的主題與settings.java的列表首選項,以便當用戶單擊列表中的主題時,主題將應用於整個應用程序? – Vishal

相關問題