下面我有一个应用程序,该应用程序允许用户选择背景颜色和字体。这将应用于其余活动。但是,这很长,而且不容易阅读。有没有办法简化我的方法?
样式
<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);
}
}
}
不,这是最好的方法。继承可以用来使主题和样式更紧凑,但并不适用于您的情况。
您可以通过使用if/else if语句在活动中提供一个更好的代码。即使将所有主题都放在哈希图中,否则,没有,没有什么更好的了。
我可能至少会将其分为另一个类,例如ThemeHandler.getThemeId(String)
,设置一些静态最终字符串而不是硬编码,然后返回R.style.whatever
。最后,将其与setTheme(ThemeHandler.getThemeId(theme));
一起应用。这样的东西:
class ThemeHandler {
public static final String RED_MONO = "RedMonospace";
public static final String RED_SANS = "RedSans";
public static final String RED_SERIF = "RedSerif";
//etc...
public static int getThemeId(String themeString) {
if(themeString.equals(RED_MONO)) return R.style.Theme_RedMono;
else if(themeString.equals(RED_SANS)) return R.style.Theme_RedSans;
else if(themeString.equals(RED_SERIF)) return R.style.Theme_RedSerif;
//etc...
else return R.style.Theme_Blue; //or whatever default
}
}
//Then, in your onCreate()
String theme = preferences.getString("THEME", "Blue");
setTheme(ThemeHandler.getThemeId(theme));
要获得额外的信用,您可以切换到使用枚举而不是字符串来简化事物的简化 - 然后,您可以使用Switch语句,而不是IF/else语句。:)