有2个主题的Android应用程序

时间:2016-05-17 10:13:14

标签: android material-design

我尝试使用2个主题创建应用程序,我只能针对一个活动执行此操作,而其他活动不再受影响,如何更改主题以应用于所有页面?

主题代码:

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">#795548</item>
    <item name="colorPrimaryDark">#5d4037</item>
    <item name="colorAccent">#e6300b</item>
    <item name="android:textColorPrimary">#ffffff</item>
  <item name="android:navigationBarColor">#e4ccc7</item>


</style>


<style name="apptheme2" parent="Theme.AppCompat.Light.NoActionBar">

    <item name="colorPrimary">#8ff2f2</item>
    <item name="colorPrimaryDark">#044949</item>
    <item name="colorAccent">#813ba4</item>
    <item name="android:textColorPrimary">#000000</item>
    <item name="android:navigationBarColor">#735852</item>

一项活动的java代码:

 Button btndark;
Button btnlight;



@Override
protected void onCreate(Bundle savedInstanceState) {

    final SharedPreferences shared = getSharedPreferences("prefers", 0);
    final Boolean theme = shared.getBoolean("theme", true);

    if (!theme) {
        setTheme(R.style.AppTheme);
    } else {

        setTheme(R.style.apptheme2);
    }

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    btndark = (Button) findViewById(R.id.btn1);
    btnlight = (Button) findViewById(R.id.btn2);


    btndark.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            SharedPreferences.Editor editor = shared.edit();
            editor.putBoolean("theme", false);
            editor.commit();
            recreate();
        }
    });


    btnlight.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            SharedPreferences.Editor editor = shared.edit();
            editor.putBoolean("theme", true);
            editor.commit();
            recreate();

        }
    });

2 个答案:

答案 0 :(得分:1)

试试这段代码: 在清单中写下这段代码

 <activity
        android:name="Activity1"
        android:theme="@style/AppTheme1" />

进行其他活动

<activity
        android:name="Activity2"
        android:theme="@style/AppTheme2" />

答案 1 :(得分:0)

在所有活动中扩展以下活动。您可以在共享首选项中保存整数。因此,您可以知道选择了哪个主题。

public class BaseActivity extends Activity {

public static int themePrimaryColor = R.color.ColorPrimary;
public static int themePrimaryColorDark = R.color.ColorPrimaryDark;
public static int themeAccentColor = R.color.colorAccent;

//for theme color
public static final int THEME_LIGHT = 1;
public static final int THEME_DARK = 2;

public static int currentTheme = R.style.AppTheme;

@Override
protected void onCreate(Bundle savedInstanceState) {
int themeColor = SettingPreference.getIntValue(SettingPreference.KEY_APP_THEME, THEME_LIGHT, this);
    selectTheme(themeColor);
    super.setTheme(currentTheme);
super.onCreate(savedInstanceState);
}

public static void selectTheme(int themeColor){
    switch (themeColor) {
        case THEME_LIGHT:
            setCurrentTheme(R.style.AppTheme);
            themePrimaryColor = R.color.ColorPrimary;
            themePrimaryColorDark = R.color.ColorPrimaryDark;
            themeAccentColor = R.color.colorAccent;

            break;

        case THEME_DARK:
            setCurrentTheme(R.style.AppTheme2);
            themePrimaryColor = R.color.color_primary2;
            themePrimaryColorDark = R.color.color_primary_dark2;
            themeAccentColor = R.color.color_accent2;
            break;


        default:
            setCurrentTheme(R.style.AppTheme);
            themePrimaryColor = R.color.ColorPrimary;
            themePrimaryColorDark = R.color.ColorPrimaryDark;
            themeAccentColor = R.color.colorAccent;
    }
}

public static int getCurrentTheme() {
    return currentTheme;
}

public static void setCurrentTheme(int currentTheme) {
    BaseAppCompatActivity.currentTheme = currentTheme;
}

}

重新启动应用程序后,您的应用程序主题将根据您的选择进行更改。