java - Android - onClick 更改应用主题

标签 java android xml styles themes

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

getApplication().setTheme(Theme.Holo)

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

最佳答案

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

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

复制博客代码以供快速引用:

假设您已经在 XML 文件 R.style.FirstThemeR.style.SecondThemeR.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;
          }
     }
}

让我们在“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;
          }
     }
}

希望对你有帮助...

编辑 1:

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

Builder.create() 中的实现是:

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

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

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

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

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

Builder 只是不调用。

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

How to change theme for AlertDialog

以下是投票最多的答案:

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

关于java - Android - onClick 更改应用主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18301554/

相关文章:

java - 如何使用 Java 读取/绘制/与另一个 Java 窗口/小程序交互

java - 编写 JUnit 测试用例时使用 SpringRunner 而不使用 SpringContext 是一个好习惯吗?

java - 如何强制 jTable 不可编辑?

java - Mockito:默认返回值

android - 从视频 ffmpeg 中提取 alpha

java - Android Studio更新SDK 26后无法实例化一个或多个类

java - html搜索和替换保留html标签

android - 如何给图片按钮添加点击效果?

c# - 将 IEnumerable<int> 作为参数传递给 WCF 服务

android - 如何将imageView放在工具栏的最左边?