Android如何在活动中更改主题

时间:2015-08-07 14:34:51

标签: android android-theme

我是Android新手,我的res / style.xml文件中有两个主题,一个是 Theme.Transparent ,另一个是 AppTheme 。我的要求是我必须在用户从按钮操作中选择时更改活动主题。

如何在res / style.xml文件中设置主题包含。我试过以下

changeTheme.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
      super.theme(......); //
    }
});

super.theme 方法不接受来自res / style.xml文件的主题?

4 个答案:

答案 0 :(得分:1)

通过在调用super onCreate之前设置它来更改活动主题:

setTheme(R.style.Theme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);

在“开启”中,单击“重新启动您的活动以应用更改:

activity.finish();
activity.startActivity(new Intent(activity, activity.getClass()));

答案 1 :(得分:1)

可以用以下方法修改它:

context.setTheme(android.R.style.Theme_Holo_Light)

默认为Theme_Holo

答案 2 :(得分:0)

你可以尝试

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);
        setTheme(android.R.style.Theme_Holo_Light_DarkActionBar);
        setContentView(R.layout.activity_main);
    }

要在按钮单击时更改主题,您可以参考以下代码:

public static void changeTheme(final Context context) {

        int intCurrentTheme = getTheme();

              final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
        dialog.setContentView(R.layout.dialog_theme);
        dialog.setTitle(R.string.titletheme);
        dialog.show();


        dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
                R.drawable.ic_theme);

        final RadioGroup radioGroup = (RadioGroup) dialog.findViewById(R.id.radioGroup);

        switch (intCurrentTheme){
            case android.R.style.Theme_Holo_Light_DarkActionBar:
                radioGroup.check(R.id.radio_theme_holo_light_darkactionbar);
                break;
            case android.R.style.Theme_Holo_Light:
                radioGroup.check(R.id.radio_theme_holo_light);
                break;
            case android.R.style.Theme_Holo:
                radioGroup.check(R.id.radio_theme_holo);
                break;
        }

        Button btnSaveTheme = (Button) dialog.findViewById(R.id.btnSaveTheme);
        Button btnCancelTheme = (Button) dialog.findViewById(R.id.btnCancelTheme);

        btnSaveTheme.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                int intSelectedId = radioGroup.getCheckedRadioButtonId();
                int intNewTheme;
                switch (intSelectedId){
                    case R.id.radio_theme_holo_light_darkactionbar:
                        intNewTheme = android.R.style.Theme_Holo_Light_DarkActionBar;
                        break;
                    case R.id.radio_theme_holo_light:
                        intNewTheme = android.R.style.Theme_Holo_Light;
                        break;
                    case R.id.radio_theme_holo:
                        intNewTheme = android.R.style.Theme_Holo;
                        break;
                    default:
                        intNewTheme = android.R.style.Theme_Holo_Light_DarkActionBar;
                }

                savePreference(context, "themePref", intNewTheme);
                dialog.dismiss();


         //Reload main activity                             
                 Intent intent = new Intent(context, MainActivity.class);
                 context.startActivity(intent);

            }
        });

        btnCancelTheme.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });
    }

答案 3 :(得分:0)

您可以重新创建活动以动态更改当前主题

public class MainActivity extends AppCompatActivity {

    static int currentTheme = 0;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (currentTheme == 0) {
            setTheme(R.style.AppTheme);
        } else {
            setTheme(R.style.AppTheme1);
        }

        Button button = new Button(this);
        button.setText("AppTheme1");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentTheme = 1;
                recreate();
            }
        });


        setContentView(button);
    }
}