Android - 点击事件更改应用主题

51

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

getApplication().setTheme(Theme.Holo)

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


1
如果您将此代码放在setContentView()之前,它将起作用,但是要在单击时更改它,请尝试使用setTheme(),然后在其后使用setContentView()。 - Mohamed_AbdAllah
你在调用 setTheme() 后又调用了 setContentView(View) 吗? - Praful Bhatnagar
尝试使用setTheme然后使用setContentView,但是没有任何变化...我可以在日志中看到android采取了onClick... - user2606414
1
你可以尝试在 setTheme() 之后重新启动该活动,看看是否有效。 - Praful Bhatnagar
1
尝试移除 getApplication() 方法(只保留 setTheme())。 - Mohamed_AbdAllah
显示剩余4条评论
1个回答

70

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

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的“非主题感知”构造函数,它看起来像这样:

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

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

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

建造者只是没有调用它。

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

如何为AlertDialog更改主题

以下是得票最多的答案:

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

ListViewжңүд»Җд№Ҳй—®йўҳеҗ—пјҹжӮЁиғҪеҗҰдҪҝз”ЁжӯӨж–№жі•жӣҙж”№йҷӨListViewд№ӢеӨ–зҡ„жүҖжңүе…¶д»–и§Ҷеӣҫзҡ„дё»йўҳпјҹ - Praful Bhatnagar
请查看以下帖子中得票最高的答案。 - Praful Bhatnagar
1
请查看我的编辑1,针对AlertDialog问题进行了修改。 - Praful Bhatnagar
3
非常好的回答。应该有更多的投票。 - Yaroslav Mytkalyk
4
仅补充上面的答案。请注意,Activity已被弃用,Android现在使用AppCompatActivity。因此,请将activity替换为AppCompatActivity。例如: changeToTheme(Activity activity, int theme) 替换为 changeToTheme(AppCompatActivity activity, int theme) 然后您可以使用以下代码: activity.recreate() - Karue Benson Karue
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接