我试图在 Holo.Light 和 Holo (Dark) 之间切换,因此我的应用程序中的所有内容都更改为用户在首选项中设置的主题。
我一直在查看一些已完成的开源应用程序,但似乎无法使其与我的项目一起使用,对此的任何帮助将不胜感激。
我在 DashboardActivity.java 中遇到的 Current ISSUE 是:
“我遇到了“setTheme(Integer.parseInt(pref.getString(”
仪表板活动(更新)
public class DashboardActivity extends Activity {
public static final int THEME_BLACK = R.style.DarkThemeAndroid;
public static final int THEME_WHITE = R.style.LightThemeAndroid;
public static final int THEME_WHITE_BLACK = android.R.style.Theme_Holo_Light_DarkActionBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// I've having issue with the "pref.getString" i've tried "Settings.getString" and get error about needing a getString method is needed in Settings.java
setTheme(Integer.parseInt( pref.getString("selectedTheme", String.valueOf(R.style.LightThemeAndroid) )));
setContentView(R.layout.dashboard_layout);
// the rest of my code
)
)
设置更新)
public class Settings extends PreferenceActivity implements
OnSharedPreferenceChangeListener {
final static String[] mThemeEntries = {
"Default (Light)",
"Dark"
};
final static String[] mThemeValues = {
String.valueOf(R.style.LightThemeAndroid),
String.valueOf(R.style.DarkThemeAndroid)
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Loads the XML preferences file.
addPreferencesFromResource(R.xml.settings);
// SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
ListPreference listPref = (ListPreference)findPreference("selectedTheme");
listPref.setEntries(mThemeEntries);
listPref.setEntryValues(mThemeValues);
listPref.setValue( pref.getString("selectedTheme", String.valueOf(mThemeValues[0]) ) );
XML 资源
主题.xml
<style name="LightThemeAndroid" parent="android:style/Theme.Holo.Light">
</style>
<style name="DarkThemeAndroid" parent="android:style/Theme.Holo">
</style>
设置.xml
<ListPreference
android:title="Themes"
android:summary="Change the UI of the application"
android:key="theme"
android:entries="@array/themesReturnValue"
android:entryValues="@array/themesDisplayWord"
android:defaultValue="Theme1" />
数组.xml
<string-array name="themesReturnValue">
<item>Light</item>
<item>Dark</item>
<item>LightActionBar</item>
</string-array>
<string-array name="themesDisplayWord">
<item>Theme1</item>
<item>Theme2</item>
<item>Theme3</item>
</string-array>