我有一个应用程序,我让用户在白天和黑夜模式之间切换,使用主题Theme.AppCompat.DayNight。然而,在切换主题时随机地,按钮具有错误的背景颜色,并且溢出按钮也会发生相同的情况(它将在浅色主题上亮起,或在暗主题上暗淡)。我已经编写了一个最小的测试应用程序,问题也可以在这里重现。
以下是示例应用:
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.Yellow">
<item name="colorAccent">@color/accent_yellow</item>
</style>
<style name="AppTheme.LightBlue">
<item name="colorAccent">@color/accent_light_blue</item>
</style>
<style name="AppTheme.Steel">
<item name="colorAccent">@color/accent_steel</item>
</style>
</resources>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="accent_light_blue">#ff5677fc</color>
<color name="accent_yellow">#ffffc107</color>
<color name="accent_steel">#ff607d8b</color>
</resources>
arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="accent_colors">
<item>@color/accent_light_blue</item>
<item>@color/accent_yellow</item>
<item>@color/accent_steel</item>
</array>
<array name="accent_themes">
<item>@style/AppTheme.LightBlue</item>
<item>@style/AppTheme.Yellow</item>
<item>@style/AppTheme.Steel</item>
</array>
</resources>
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.quicosoft.daynighttest.MainActivity"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="24dp">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Switch theme"
tools:layout_editor_absoluteX="143dp"
tools:layout_editor_absoluteY="262dp"
app:layout_constraintLeft_toLeftOf="@+id/activity_main"
tools:layout_constraintLeft_creator="0"
app:layout_constraintTop_toTopOf="@+id/activity_main"
tools:layout_constraintTop_creator="0"
app:layout_constraintRight_toRightOf="@+id/activity_main"
tools:layout_constraintRight_creator="0"
app:layout_constraintBottom_toBottomOf="@+id/activity_main"
tools:layout_constraintBottom_creator="0"
app:layout_constraintVertical_bias="0.43" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checkbox"
android:checked="true"
tools:layout_editor_absoluteX="159dp"
tools:layout_editor_absoluteY="214dp"
app:layout_constraintLeft_toLeftOf="@+id/button"
tools:layout_constraintLeft_creator="0"
app:layout_constraintRight_toRightOf="@+id/button"
tools:layout_constraintRight_creator="0"
app:layout_constraintBottom_toTopOf="@+id/button"
android:layout_marginBottom="16dp"
tools:layout_constraintBottom_creator="0" />
</android.support.constraint.ConstraintLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
final boolean night = prefs.getBoolean("mode", false);
AppCompatDelegate.setDefaultNightMode(night ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO);
TypedArray ar = getResources().obtainTypedArray(R.array.accent_themes);
int len = ar.length();
int[] themes = new int[len];
for (int i = 0; i < len; themes[i] = ar.getResourceId(i++, 0)) ;
ar.recycle();
int colorIndex = new Random().nextInt(len);
setTheme(themes[colorIndex]);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
prefs.edit().putBoolean("mode", !night).apply();
finish();
startActivity(getIntent());
}
});
}
}
这是主题正确切换时的按钮:
这是主题切换不正确时的按钮:
作为参考,这里是一个正确的夜间主题(问题发生在白天和夜晚模式,日主题将有一个深灰色背景而不是浅灰色的按钮,夜间主题将具有浅灰色背景而不是深灰色 - 就像按钮保留前一主题的背景,但内部文本切换到正确的主题):
有时为什么按钮背景有错误的想法?