下面我有一个应用程序,它允许用户选择背景颜色和字体。这将应用于其余的活动。然而,这是非常长的,不是很容易阅读。有没有办法简化我的方法?
风格
<style name ="redBodyMono">
<item name="android:textColor">@color/red</item>
<item name ="android:background">@color/white</item>
<item name="android:typeface">monospace</item>
</style>
<style name ="redTitleMono">
<item name="android:textColor">@color/white</item>
<item name ="android:background">@color/red</item>
<item name="android:typeface">monospace</item>
</style>
<style name ="blueBodyMono">
<item name="android:textColor">@color/darkBlue</item>
<item name ="android:background">@color/white</item>
<item name="android:typeface">monospace</item>
</style>
<style name ="blueTitleMono">
<item name="android:textColor">@color/white</item>
<item name ="android:background">@color/darkBlue</item>
<item name="android:typeface">monospace</item>
</style>
<style name ="greenBodyMono">
<item name="android:textColor">@color/green</item>
<item name ="android:background">@color/white</item>
<item name="android:typeface">monospace</item>
</style>
<style name ="greenTitleMono">
<item name="android:textColor">@color/white</item>
<item name ="android:background">@color/green</item>
<item name="android:typeface">monospace</item>
</style>
<style name ="redBodySans_Serif">
<item name="android:textColor">@color/red</item>
<item name ="android:background">@color/white</item>
<item name="android:typeface">sans</item>
</style>
<style name ="redTitleSans_Serif">
<item name="android:textColor">@color/white</item>
<item name ="android:background">@color/red</item>
<item name="android:typeface">sans</item>
</style>
<style name ="blueBodySans_Serif">
<item name="android:textColor">@color/darkBlue</item>
<item name ="android:background">@color/white</item>
<item name="android:typeface">sans</item>
</style>
<style name ="blueTitleSans_Serif">
<item name="android:textColor">@color/white</item>
<item name ="android:background">@color/darkBlue</item>
<item name="android:typeface">sans</item>
</style>
<style name ="greenBodySans_Serif">
<item name="android:textColor">@color/green</item>
<item name ="android:background">@color/white</item>
<item name="android:typeface">sans</item>
</style>
<style name ="greenTitleSans_Serif">
<item name="android:textColor">@color/white</item>
<item name ="android:background">@color/green</item>
<item name="android:typeface">sans</item>
</style>
<style name ="redBodySerif">
<item name="android:textColor">@color/red</item>
<item name ="android:background">@color/white</item>
<item name="android:typeface">serif</item>
</style>
<style name ="redTitleSerif">
<item name="android:textColor">@color/white</item>
<item name ="android:background">@color/red</item>
<item name="android:typeface">serif</item>
</style>
<style name ="blueBodySerif">
<item name="android:textColor">@color/darkBlue</item>
<item name ="android:background">@color/white</item>
<item name="android:typeface">serif</item>
</style>
<style name ="blueTitleSerif">
<item name="android:textColor">@color/white</item>
<item name ="android:background">@color/darkBlue</item>
<item name="android:typeface">serif</item>
</style>
<style name ="greenBodySerif">
<item name="android:textColor">@color/green</item>
<item name ="android:background">@color/white</item>
<item name="android:typeface">serif</item>
</style>
<style name ="greenTitleSerif">
<item name="android:textColor">@color/white</item>
<item name ="android:background">@color/green</item>
<item name="android:typeface">serif</item>
</style>
<style name = "Theme" parent="@android:style/Theme">
</style>
<style name = "Theme.RedMono" >
<item name="textTitle">@style/redTitleMono</item>
<item name="textBody">@style/redBodyMono</item>
</style>
<style name = "Theme.BlueMono" >
<item name="textTitle">@style/blueTitleMono</item>
<item name="textBody">@style/blueBodyMono</item>
</style>
<style name = "Theme.GreenMono">
<item name="textTitle">@style/greenTitleMono</item>
<item name="textBody">@style/greenBodyMono</item>
</style>
<style name = "Theme.RedSans" >
<item name="textTitle">@style/redTitleSans_Serif</item>
<item name="textBody">@style/redBodySans_Serif</item>
</style>
<style name = "Theme.BlueSans" >
<item name="textTitle">@style/blueTitleSans_Serif</item>
<item name="textBody">@style/blueBodySans_Serif</item>
</style>
<style name = "Theme.GreenSans">
<item name="textTitle">@style/greenTitleSans_Serif</item>
<item name="textBody">@style/greenBodySans_Serif</item>
</style>
<style name = "Theme.RedSerif" >
<item name="textTitle">@style/redTitleSerif</item>
<item name="textBody">@style/redBodySerif</item>
</style>
<style name = "Theme.BlueSerif" >
<item name="textTitle">@style/blueTitleSerif</item>
<item name="textBody">@style/blueBodySerif</item>
</style>
<style name = "Theme.GreenSerif">
<item name="textTitle">@style/greenTitleSerif</item>
<item name="textBody">@style/greenBodySerif</item>
</style>
菜单类
public class UserMenu extends Activity implements OnClickListener {
Button preview, apply;
Spinner spinnerColor, spinnerTypeFace;
SharedPreferences preferences;
public static String theme = "Blue";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
preferences = getSharedPreferences("StylePref", 0);
theme = preferences.getString("THEME", "Blue");
if(theme.equals("RedMonospace"))
{
setTheme(R.style.Theme_RedMono);
}
if(theme.equals("RedSerif"))
{
setTheme(R.style.Theme_RedSans);
}
if(theme.equals("RedSans"))
{
setTheme(R.style.Theme_RedSerif);
}
if(theme.equals("BlueMonospace"))
{
setTheme(R.style.Theme_BlueMono);
}
if(theme.equals("BlueSerif"))
{
setTheme(R.style.Theme_BlueSans);
}
if(theme.equals("BlueSans"))
{
setTheme(R.style.Theme_BlueSerif);
}
if(theme.equals("GreenMonospace"))
{
setTheme(R.style.Theme_GreenMono);
}
if(theme.equals("GreenSerif"))
{
setTheme(R.style.Theme_GreenSans);
}
if(theme.equals("GreenSans"))
{
setTheme(R.style.Theme_GreenSerif);
}
setContentView(R.layout.activity_user_menu);
System.out.println("tHE THEME " + theme);
spinnerColor = (Spinner) findViewById(R.id.spinnerColorMenu);
spinnerTypeFace = (Spinner) findViewById(R.id.spinnerTypeFaceMenu);
preview = (Button)findViewById(R.id.previewButton);
preview.setOnClickListener(this);
apply = (Button)findViewById(R.id.applyButton);
apply.setOnClickListener(this);
}
public void onClick(View v)
{
if(v== preview)
{
SharedPreferences.Editor preferencesEditor = preferences.edit();
String bgColor = spinnerColor.getSelectedItem().toString();
String typeFace = spinnerTypeFace.getSelectedItem().toString();
System.out.println(typeFace);
preferencesEditor.putString("THEME", bgColor+typeFace);
preferencesEditor.commit();
if(bgColor.equals("Red") && typeFace.equals("Monospace"))
{
Intent intent = getIntent();
//intent.putExtra("Theme", "Red");
finish();
startActivity(intent);
}
if(bgColor.equals("Red") && typeFace.equals("Sans"))
{
Intent intent = getIntent();
//intent.putExtra("Theme", "Red");
finish();
startActivity(intent);
}
if(bgColor.equals("Red") && typeFace.equals("Serif"))
{
Intent intent = getIntent();
//intent.putExtra("Theme", "Red");
finish();
startActivity(intent);
}
else if(bgColor.equals("Blue")&& typeFace.equals("Monospace"))
{
Intent intent = getIntent();
//intent.putExtra("Theme", "Blue");
finish();
startActivity(intent);
}
else if(bgColor.equals("Blue")&& typeFace.equals("Sans"))
{
Intent intent = getIntent();
//intent.putExtra("Theme", "Blue");
finish();
startActivity(intent);
}
else if(bgColor.equals("Blue")&& typeFace.equals("Serif"))
{
Intent intent = getIntent();
//intent.putExtra("Theme", "Blue");
finish();
startActivity(intent);
}
else if(bgColor.equals("Green")&& typeFace.equals("Monospace"))
{
Intent intent = getIntent();
//intent.putExtra("Theme", "Blue");
finish();
startActivity(intent);
}
else if(bgColor.equals("Green")&& typeFace.equals("Sans"))
{
Intent intent = getIntent();
//intent.putExtra("Theme", "Blue");
finish();
startActivity(intent);
}
else if(bgColor.equals("Green")&& typeFace.equals("Serif"))
{
Intent intent = getIntent();
//intent.putExtra("Theme", "Blue");
finish();
startActivity(intent);
}
}
else if(v == apply)
{
SharedPreferences.Editor preferencesEditor = preferences.edit();
String bgColor = spinnerColor.getSelectedItem().toString();
String typeFace = spinnerTypeFace.getSelectedItem().toString();
preferencesEditor.putString("THEME", bgColor+typeFace);
preferencesEditor.commit();
Intent applyIntent = new Intent(this,MyCourses.class);
startActivity(applyIntent);
}
}
}