1

下面我有一个应用程序,它允许用户选择背景颜色和字体。这将应用于其余的活动。然而,这是非常长的,不是很容易阅读。有没有办法简化我的方法?

风格

<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);

        }
}
}
4

2 回答 2

0

不,这是最好的方法。继承可用于使主题和样式更紧凑,但它并不真正适用于您的情况。

您可以通过使用 if/else if 语句在活动中提供更好的代码。即使将所有主题都放在哈希图中,否则,没有什么比这更好的了。

于 2012-11-07T19:44:59.360 回答
0

我可能至少会将它分解为另一个类,例如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 语句。:)

于 2012-11-07T20:22:17.043 回答