Android - 在onClick上更改应用主题

use*_*414 48 java xml android themes styles

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

getApplication().setTheme(Theme.Holo)
Run Code Online (Sandbox Code Playgroud)

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

Pra*_*gar 65

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

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

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

假设您已经在XML文件中定义了以下三个主题R.style.FirstTheme,R.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;
          }
     }
}
Run Code Online (Sandbox Code Playgroud)

让我们在"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;
          }
     }
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你...

编辑1:

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

Builder.create()中的实现是:

public AlertDialog create() {
    final AlertDialog dialog = new AlertDialog(P.mContext);
    P.apply(dialog.mAlert);
    [...]
}
Run Code Online (Sandbox Code Playgroud)

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

protected AlertDialog(Context context) {
    this(context, com.android.internal.R.style.Theme_Dialog_Alert);
}
Run Code Online (Sandbox Code Playgroud)

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

protected AlertDialog(Context context, int theme) {
    super(context, theme);
    [...]
}
Run Code Online (Sandbox Code Playgroud)

Builder只是不打电话.

查看以下帖子了解更多相关修复程序..

如何更改AlertDialog的主题

以下是投票最多的答案:

  new AlertDialog.Builder(
  new ContextThemeWrapper(context, android.R.style.Theme_Dialog))
Run Code Online (Sandbox Code Playgroud)

  • 只是从上面的答案添加一些东西.请注意,不推荐使用Activity,而android现在正在使用AppCompatActivity.所以用AppCompatActivity替换活动.例如`changeToTheme(Activity activity,int theme)`更改为`changeToTheme(AppCompatActivity activity,int theme)`然后你可以说:`activity.recreate()` (4认同)
  • 非常好的答案.应该有更多的选票. (3认同)