我在styles.xml 中定义了两个主题:
<style name="AppTheme" parent="android:Theme.Holo"></style>
<style name="AppLightTheme" parent="android:Theme.Holo.Light">
<item name="android:background">#FFFFFF</item>
</style>
Run Code Online (Sandbox Code Playgroud)
这是我设置活动主题的方式:
protected void changeTheme(boolean dark) {
if (dark) {
setTheme(R.style.AppTheme);
} else {
setTheme(R.style.AppLightTheme);
}
}
Run Code Online (Sandbox Code Playgroud)
现在,在我更改主题后,只有背景保持不变,直到我打开不同的布局并返回。我正在使用 DrawerLayout 所以我基本上在布局之间切换。
我怎样才能重新绘制它或刷新它?
这是我尝试过但没有做任何事情的所有内容:
ViewGroup vg = findViewById (R.id.mainLayout);
vg.invalidate();
Run Code Online (Sandbox Code Playgroud)
.
Intent intent = getIntent(); //this one is obvious, had to include so you don't try this unnecesarry.. code
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
.
getWindow().getDecorView().findViewById(android.R.id.content).invalidate();
Run Code Online (Sandbox Code Playgroud)
.
getWindow().getDecorView().findViewById(android.R.id.content).refreshDrawableState();
Run Code Online (Sandbox Code Playgroud)
.
getWindow().getDecorView().findViewById(android.R.id.content).requestLayout();
Run Code Online (Sandbox Code Playgroud)
.
findViewById(android.R.id.content).invalidate();
Run Code Online (Sandbox Code Playgroud)
.
myLayout.invalidate();
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
您可以调用recreate()来重新创建您的活动并使用新主题重新创建所有视图。
protected void changeTheme(boolean dark) {
if (dark) {
setTheme(R.style.AppTheme);
} else {
setTheme(R.style.AppLightTheme);
}
recreate();
}
Run Code Online (Sandbox Code Playgroud)