在我的应用程序(API 21)中,用户可以在Dark和Light主题之间切换。 问题是当主题更改活动背景不遵循新主题时! 我试过了:
我制作了一个示例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>
答案 0 :(得分:3)
您只需在setTheme(themeid)
之前移动super.onCreate(savedInstanceState)
即可。