我想在我的小型应用程序中启用黑暗模式。
因此,我在 styles.xml
中将样式更改为 Daynightstyles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
在MainAcitivity中,我执行了以下操作:
MainAcitivty.java
public class MainActivity extends AppCompatActivity {
Button darkmode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
darkmode=findViewById(R.id.darkmode);
darkmode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
startActivity(new Intent(getApplicationContext(),MainActivity.class));
finish();
}
});
但是,输出如下所示:
当我运行应用程序时,它显示以下内容:
当我单击黑暗模式按钮时,它显示如下:
因此,仅按钮被更改。不是应用程序的所有部分。为什么?