简体   繁体   English

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

[英]changing theme in java doesn't change background-color

I'm trying to change the theme on runtime with java-code, because I want to have the user be able to change the app-theme via the preferences-menu. 我正在尝试使用java代码更改运行时的主题,因为我希望用户能够通过首选项菜单更改应用程序主题。 so, I let the user the theme, and then read the results like this: 所以,我让用户主题,然后读取结果如下:

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

unfortunately this doesnt work for some reason.... the font-color slightly shifts from darker gray (light theme) to a brighter gray (black theme) but the background always stays white/black (depending on which theme wes initially selected in the manifest-file) 不幸的是,由于某种原因,这不起作用....字体颜色从深灰色(浅色主题)略微转变为更亮的灰色(黑色主题)但背景总是保持白色/黑色(取决于最初在清单文件)

If I completely remove the theme-entry in the manifest file, then its as if I would have selected the black-theme.... 如果我完全删除清单文件中的主题条目,那就好像我会选择黑色主题....

....is there something I'm overlooking? ....我有什么东西可以俯瞰吗?

I had the same problem and I solved in this way.. 我有同样的问题,我这样解决了..

@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

}

this code is for recreate the Activity saving Bundle and changing the theme. 此代码用于重新创建活动保存包并更改主题。 You have to write your own onSaveInstanceState(Bundle outState); 你必须编写自己的onSaveInstanceState(Bundle outState); From API-11 you can use the method recreate() instead 从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();

You can't set the app theme at runtime. 您无法在运行时设置应用程序主题。 If you want to change the the theme everywhere in your application, you must call 如果要在应用程序中的任何位置更改主题,则必须调用

setTheme(resId) setTheme(渣油)

as a first thing in Activity's onCreate(). 作为Activity的onCreate()中的第一件事。

Eg: 例如:

@Override public void onCreate(Bundle savedInstanceState) { setTheme(resId) } @Override public void onCreate(Bundle savedInstanceState){setTheme(resId)}

If you want to change the theme of already started activities, then you have to re-create them. 如果要更改已启动活动的主题,则必须重新创建它们。

This is a known issue: https://code.google.com/p/android/issues/detail?id=3793 这是一个众所周知的问题: https//code.google.com/p/android/issues/detail?id = 3793

You have to change the background color manually, setTheme() will not change it. 您必须手动更改背景颜色, setTheme()不会更改它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM