我实际上要做的是在用户从我的 ListPreference 中选择适当的选项后更改布局和文本背景颜色
public class PrefFragment extends PreferenceFragment implements OnPreferenceChangeListener {
private static final String PREF_THEME="themeset";
ListPreference theme_selector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
theme_selector = (ListPreference) findPreference(PREF_THEME);
theme_selector.setOnPreferenceChangeListener(this);
};
public boolean onPreferenceChange(Preference preference, Object newValue) {
return true;
}
}
那是我的首选片段
这是我的preferences.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<ListPreference
android:key="themeset"
android:entries="@array/themeselection"
android:summary="@string/pref_theme_summary"
android:entryValues="@array/themeValues"
android:title="@string/pref_themes"
android:defaultValue="1"/>
这是我的 array.xml
<resources>
<string-array name="themeselection">
<item name="Black">Black</item>
<item name="White7">White</item>
</string-array>
<string-array name="themeValues">
<item name="Black">1</item>
<item name="White">2</item>
</string-array>
</resources>
以下是我的主要活动
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.graphics.Color;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity implements OnSharedPreferenceChangeListener {
private int theme_setting;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout theme =(RelativeLayout) findViewById(R.id.layout);
TextView txtcolor = (TextView) findViewById(R.id.txt);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);
theme_setting = Integer.parseInt((prefs.getString("themeset", "1")));
if (theme_setting == 1)
{
theme.setBackgroundColor(Color.parseColor("#000000"));
txtcolor.setTextColor(Color.parseColor("#FFFFFF"));
this.recreate();
}
else
{
theme.setBackgroundColor(Color.parseColor("#FFFFFF"));
txtcolor.setTextColor(Color.parseColor("#000000"));
this.recreate();
}
}
@Override
public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {
if (arg1.equals("themeValues"))
{
theme_setting = Integer.parseInt((arg0.getString("themeset", "1")));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
Intent i = new Intent(this, PrefSummary.class);
startActivityForResult(i,1);
break;
}
return true;
}
}
因此,正如我之前所说,我想做的只是通过以编程方式设置/更改布局背景颜色和字体颜色来更改主题,对于上面的代码,我得到一个冻结的白色应用程序屏幕,就好像布局没有加载一样。我意识到我主要在主要活动中做了一些可怕的错误,我希望得到一些帮助以使其正常工作。