Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在运行时从Theme.AppCompat更改为Theme.AppCompat.Light.darkaActionBar_Android_Themes_Runtime_Android Appcompat - Fatal编程技术网

Android 在运行时从Theme.AppCompat更改为Theme.AppCompat.Light.darkaActionBar

Android 在运行时从Theme.AppCompat更改为Theme.AppCompat.Light.darkaActionBar,android,themes,runtime,android-appcompat,Android,Themes,Runtime,Android Appcompat,我的应用程序中有一个设置,用户可以在其中更改主题。可以通过导航抽屉访问设置,因为我想要一个“汉堡包菜单图标”,所以我不得不从基本活动扩展AppCompatActivity My styles.xml非常简单: <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> </style> <style name="DarkTheme" parent="Theme.AppCompat">

我的应用程序中有一个设置,用户可以在其中更改主题。可以通过导航抽屉访问设置,因为我想要一个“汉堡包菜单图标”,所以我不得不从基本活动扩展AppCompatActivity

My styles.xml非常简单:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
</style>
<style name="DarkTheme" parent="Theme.AppCompat">
</style>
以前,当我只扩展活动(并使用常规的材料主题)时,效果很好。但是,现在当我扩展AppCompatActivity(并使用AppCompat主题)时,它不起作用

文本颜色根据我更改的主题而变化,但屏幕和操作栏的背景颜色与AndroidManifest中的背景颜色相同(在我的例子中为暗色调)


为什么仅仅因为我使用了AppCompat,它就突然不同了?如何修复它?

调用setTheme(R.style.AppTheme);在super.OnCreate()方法之前,如果我把它放在super.OnCreate行之前,我会遇到另一个问题。我的操作栏上方有一个白色条。它只在我切换到AppTheme时出现,而不是在我切换到DarkTheme时出现。我解决了它。当我将AppTheme的名称改为LightTheme时,新问题消失了。我不知道为什么android会因为这个名字而表现得很奇怪,但它确实做到了;在super.OnCreate()方法之前,如果我把它放在super.OnCreate行之前,我会遇到另一个问题。我的操作栏上方有一个白色条。它只在我切换到AppTheme时出现,而不是在我切换到DarkTheme时出现。我解决了它。当我将AppTheme的名称改为LightTheme时,新问题消失了。我不知道为什么安卓会因为这个名字而表现得很奇怪,但确实如此。
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    sharedPreferences.registerOnSharedPreferenceChangeListener(MainActivityTransmit.this);
    Theme = sharedPreferences.getString("theme_preference","1");

    switch(Theme) {
        case "AppTheme":
            setTheme(R.style.AppTheme);
            break;
        case "DarkTheme":
            setTheme(R.style.DarkTheme);
            break;
        default:
            break;
    }

    setContentView(R.layout.activity_main_transmit);