如何在一个按钮上将主题设置为暗

How to setTheme to dark on one Button

小伙伴们可以通过ThemeUtils一键设置主题吗??查看屏幕截图,只需一个按钮 'invert' 即可将浅色设置为深色,将深色设置为浅色?如果可能怎么办??请帮助我

我的代码:

case R.id.Invert:
            ThemeUtils.setTheme(this, "dark");
            return true;

看这张图片:https://drive.google.com/file/d/0B5SpWSpauPDuSGtWREgybnB6aEk/view?usp=drivesdk

在调用 SetContentView

之前,您必须在 Activity 上调用 setTheme 方法

因此,要更改已打开的 Activity 的主题,您需要重新启动它。

例如:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(YOUR_THEME_FROM_SHARED_PREFS);
    setContentView(...)
}

case R.id.Invert:
            // Set theme in shared Prefs here

            this.recreate(); // restart the activity
            return true;

是的,当然。定义主题,你要在style文件夹中设置。而不是你的字符串写代码 R.style.YourOwnTheme