这是我第一次尝试使用偏好来更改应用主题,而我似乎无法正常使用。这是我的代码:
MainActivity.Java onCreate方法
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String values = getPrefs.getString("list","1");
if(values.contentEquals("1")){
setTheme(R.style.Holo_Dark);
}else if(values.contentEquals("2")){
setTheme(R.style.AppTheme);
}
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
TextView medEntry = (TextView)findViewById(R.id.med_name);
/* medEntry.setOnFocusListener(new FocusListner(){
}); */
}
Prefs.Java:
public class Prefs extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
prefs的array.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="list">
<item>Dark</item>
<item>Light</item>
</string-array>
<string-array name="lValues">
<item>1</item>
<item>2</item>
</string-array>
</resources>
styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="Holo_Dark" parent="android:Theme.Holo">
</style>
</resources>
和我的清单:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Prefs"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.PREFS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
答案 0 :(得分:0)
使用此代码
if(values.equals("1")
而不是
if(values.contentEquals("1"))
答案 1 :(得分:0)
提供一个String,可以设置一个常量来最小化拼写错误的风险。 如果你在prefs中搞砸了什么,请不要忘记取消并重新安装应用程序。