Java Android - 在 onClick 上更改应用主题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18301554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:17:10  来源:igfitidea点击:

Android - Change app Theme on onClick

javaandroidxmlstylesthemes

提问by user2606414

i know there is a way to change the app default theme on button click. Blackmart developers have done it. I've already searched Google like 1000 pages but i found just this (not working)

我知道有一种方法可以在单击按钮时更改应用程序默认主题。Blackmart 的开发人员已经做到了。我已经在谷歌上搜索了 1000 页,但我发现了这个(不起作用)

getApplication().setTheme(Theme.Holo)

As i already created a new style in res/values/styles.xml is there any other way to dynamically change it? Even rebooting the application?

由于我已经在 res/values/styles.xml 中创建了一个新样式,还有其他方法可以动态更改它吗?甚至重新启动应用程序?

回答by Praful Bhatnagar

Following blog can solve your problem:

以下博客可以解决您的问题:

http://mrbool.com/how-to-change-the-layout-theme-of-an-android-application/25837

http://mrbool.com/how-to-change-the-layout-theme-of-an-android-application/25837

Copying the blog code for quick reference:

复制博客代码以供快速参考:

Assuming that you already defined following three themes in the XML file R.style.FirstTheme, R.style.SecondThemeand R.style.ThirdTheme

假设您已经在 XML 文件中定义了以下三个主题R.style.FirstThemeR.style.SecondTheme并且R.style.ThirdTheme

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class ChangeThemeActivity extends Activity implements OnClickListener
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Utils.onActivityCreateSetTheme(this);
        setContentView(R.layout.main);

                    findViewById(R.id.button1).setOnClickListener(this);
          findViewById(R.id.button2).setOnClickListener(this);
          findViewById(R.id.button3).setOnClickListener(this);
    }
     @Override
     public void onClick(View v)
     {
          // TODO Auto-generated method stub
          switch (v.getId())
          {
          case R.id.button1:
          Utils.changeToTheme(this, Utils.THEME_DEFAULT);
          break;
          case R.id.button2:
          Utils.changeToTheme(this, Utils.THEME_WHITE);
          break;
          case R.id.button3:
          Utils.changeToTheme(this, Utils.THEME_BLUE);
          break;
          }
     }
}

Let us write the below code in the "Utils" file:

让我们在“Utils”文件中编写以下代码:

import android.app.Activity;
import android.content.Intent;
public class Utils
{
     private static int sTheme;
     public final static int THEME_DEFAULT = 0;
     public final static int THEME_WHITE = 1;
     public final static int THEME_BLUE = 2;
     /**
      * Set the theme of the Activity, and restart it by creating a new Activity of the same type.
      */
     public static void changeToTheme(Activity activity, int theme)
     {
          sTheme = theme;
          activity.finish();
activity.startActivity(new Intent(activity, activity.getClass()));
     }
     /** Set the theme of the activity, according to the configuration. */
     public static void onActivityCreateSetTheme(Activity activity)
     {
          switch (sTheme)
          {
          default:
          case THEME_DEFAULT:
              activity.setTheme(R.style.FirstTheme);
              break;
          case THEME_WHITE:
              activity.setTheme(R.style.SecondTheme);
              break;
          case THEME_BLUE:
              activity.setTheme(R.style.Thirdheme);
              break;
          }
     }
}

Hope it helps...

希望能帮助到你...

EDIT 1:

编辑 1:

following is the reason AlertDialogdoes not take custom theme:

以下是AlertDialog不带自定义主题的原因:

Implementation in Builder.create() is:

Builder.create() 中的实现是:

public AlertDialog create() {
    final AlertDialog dialog = new AlertDialog(P.mContext);
    P.apply(dialog.mAlert);
    [...]
}

which calls the "not-theme-aware" constructor of AlertDialog, which looks like this:

它调用 AlertDialog 的“not-theme-aware”构造函数,如下所示:

protected AlertDialog(Context context) {
    this(context, com.android.internal.R.style.Theme_Dialog_Alert);
}

There is a second constructor in AlertDialog for changing themes:

AlertDialog 中有第二个构造函数用于更改主题:

protected AlertDialog(Context context, int theme) {
    super(context, theme);
    [...]
}

that the Builder just doesn't call.

Builder 只是不调用。

Check out following post for more relevant fixes..

查看以下帖子以获取更多相关修复。

How to change theme for AlertDialog

如何更改 AlertDialog 的主题

Following is the most voted answer:

以下是投票最多的答案:

  new AlertDialog.Builder(
  new ContextThemeWrapper(context, android.R.style.Theme_Dialog))