我正在尝试使用 java 代码在运行时更改主题,因为我希望用户能够通过首选项菜单更改应用程序主题。 所以,我让用户输入主题,然后读取结果如下:
if (...) {
getApplication().setTheme(R.style.BlackTheme);
} else {
getApplication().setTheme(R.style.LightTheme);
}
不幸的是,由于某种原因,这不起作用.... 字体颜色从较深的灰色(浅色主题)略微转变为较亮的灰色(黑色主题) 但背景始终保持白色/黑色(取决于最初在 list 文件中选择的主题)
如果我完全删除 list 文件中的主题条目,那么就好像我会选择黑色主题....
....我忽略了什么吗?
最佳答案
我遇到了同样的问题,我用这种方式解决了..
@Override
public void onCreate(Bundle savedInstanceState) {
if (getIntent().hasExtra("bundle") && savedInstanceState==null){
savedInstanceState = getIntent().getExtras().getBundle("bundle");
}
//add code for theme
switch(theme)
{
case LIGHT:
setTheme(R.style.LightTheme);
break;
case BLACK:
setTheme(R.style.BlackTheme);
break;
default:
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//code
}
此代码用于重新创建 Activity 保存 Bundle 并更改主题。 你必须自己写 onSaveInstanceState(Bundle outState); 从 API-11 开始,您可以改用方法 recreate()
Bundle temp_bundle = new Bundle();
onSaveInstanceState(temp_bundle);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("bundle", temp_bundle);
startActivity(intent);
finish();
关于android - 在java中改变主题不会改变背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7472765/