微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 在java中更改主题不会更改背景颜色

我正在尝试使用 java代码更改运行时的主题,因为我希望用户能够通过首选项菜单更改应用程序主题.
所以,我让用户主题,然后读取结果如下:

if (...) {
    getApplication().setTheme(R.style.BlackTheme);
} else {
    getApplication().setTheme(R.style.LightTheme);
}

不幸的是,由于某种原因,这不起作用….
字体颜色从深灰色(浅色主题)略微转变为更亮的灰色(黑色主题)
但背景总是保持白/黑(取决于最初在清单文件中选择的主题)

如果我完全删除清单文件中的主题条目,那就好像我会选择黑色主题….

….我有什么东西可以俯瞰吗?

解决方法

我有同样的问题,我这样解决了..

@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

}

代码用于重新创建活动保存包并更改主题.
你必须编写自己的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();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐