动态 Appcompat 主题导致 Activity 背景失败

Dynamic Appcompat Theme cause Activity background fail

在我的 App (API 21) 中,用户可以在深色和浅色主题之间切换。 问题是主题更改 activity 背景不遵循新主题! 我试过了:

我做了一个示例App来模拟这个问题 一些图片会解释我的意思

这是在 androidmanifest 中设置时的默认 DarkTheme :

这是在 androidmanifest 中设置时默认的 LightTheme :

这是当用户从 DarkTheme 切换到 Light Theme 时我得到的结果

这是当用户从 LightTheme 切换到 DarkTheme 时我得到的

这是代码文件:

androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.examples.testtheme" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/LightTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

main_Activity.xml(布局文件)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView android:text="Sample TextView" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large TextView Sample"
        android:textAppearance="?android:textAppearanceLarge"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        style="?android:starStyle"
        android:text="Like it? "/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceMedium"
        android:text="Try Change Theme..."/>


</LinearLayout>

MainActivity.class (java class)

public class MainActivity extends ActionBarActivity {
    static int themeid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.w("oncreate","static value is " + themeid);
        super.setTheme(themeid);
        this.setTheme(themeid);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        if (id == R.id.action_theme_dark) {
            super.setTheme(R.style.DarkTheme);
            themeid = R.style.DarkTheme;
            Log.w("optionmenuselect","Dark ID= " + R.style.DarkTheme);
            this.recreate();
        }
        if (id == R.id.action_theme_light){
            super.setTheme(R.style.LightTheme);
            themeid = R.style.LightTheme;
            Log.w("optionmenuselect","Light ID= " + R.style.LightTheme);
            this.recreate();
        }

        return super.onOptionsItemSelected(item);
    }
}

menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
    <item android:id="@+id/action_settings" android:title="@string/action_settings"
        android:orderInCategory="100" app:showAsAction="never" />
    <item android:id="@+id/action_theme_light" android:title="Light Theme"/>
    <item android:id="@+id/action_theme_dark" android:title="Dark Theme"/>
</menu>

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="LightTheme" parent="Theme.AppCompat.Light" >
        <!-- Customize your theme here. -->
    </style>
    <style name="DarkTheme" parent="Theme.AppCompat">

    </style>

</resources>

您只需将 setTheme(themeid) 移动到 super.onCreate(savedInstanceState) 之前。