我正在嘗試爲應用程序添加主題支持。它正在工作,但我希望用戶在主題之間進行選擇。問題是,我做不到,我嘗試了很多東西。我使用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>
我會感激如果有人能幫助我嗎?
我是新來的應用程序編程。我學會了創建列表首選項,然後我學會了使用樣式創建主題。然後我不知道如何鏈接列表首選項與主題。我知道如何使用onclicklistener作爲按鈕,但idk關於listpreferences。你能告訴我在哪裏放置你已經給的onItemselected的代碼嗎?而上面的代碼,它放在哪裏?我不知道使用utils,在此之前我嘗試了很多東西,其中一個有util,所以我創建了util活動。它在一定程度上工作,但不鏈接到列表首選項我手動必須更改util內的值。 – Vishal
任何人都可以告訴我,我如何鏈接我的styles.xml中的主題與settings.java的列表首選項,以便當用戶單擊列表中的主題時,主題將應用於整個應用程序? – Vishal