暗模式不起作用,不适用于整个应用程序

时间:2020-09-11 08:41:49

标签: android android-studio android-darkmode

我想在我的小型应用程序中启用黑暗模式。

因此,我在 styles.xml

中将样式更改为 Daynight

styles.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();
            }
        });

但是,输出如下所示:

当我运行应用程序时,它显示以下内容:

enter image description here

当我单击黑暗模式按钮时,它显示如下:

enter image description here

因此,仅按钮被更改。不是应用程序的所有部分。为什么?

0 个答案:

没有答案