如何正确使暗模式设置菜单起作用?

问题描述

因此,我完成了Google在Udacity上的Kotlin课程的第一堂课,此后我继续对应用程序进行了自己的更改,使它们看起来更好(只是学习,我不在乎该应用程序) 。我做了一些更改,例如添加了暗模式和自定义UI。

我希望黑暗模式菜单正常工作,例如当我单击light mode时,它应该在启用了明亮模式的情况下重新创建活动,并且还应该检查明亮模式MenutItem。我还希望该应用程序记住设置,以便如果我在该应用程序的一个会话期间选择Follow System然后关闭该应用程序,即使在打开该应用程序后,它也应继续跟踪系统。我认为应该使用SharedPreferences完成此操作,但我不知道如何正确执行此操作。正如您在下面的视频中看到的那样,当我单击任何暗模式设置选项时,它们不会自动更改设置,我需要重新启动应用程序,然后才看到与活动相匹配的活动中的任何更改关闭应用程序之前我选择的任何选项。

注意:我想提到的是,我对Android开发或Kotlin的经验不是很丰富,我只是2个月前才开始学习。我还想提一提,我并不关心应用程序本身,而是使用此应用程序来学习如何完成任务。我不在乎我的应用程序是否具备我的所有功能 如果我不学习如何做这些事情,我会希望它拥有。我所关心的不是应用程序而是我从中获得的知识,也就是说,如果您有答案,请解释一下它的工作原理。

此视频显示了该应用程序的行为:https://drive.google.com/file/d/1SnN0r351OGF4xQNCtI1wtgouSFVROKzg/view?usp=sharing

我在主要(也是唯一的)活动的布局文件添加toolbar。这是布局资源的xml代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/diceRollerActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"

    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/activity_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:title="@string/app_name" />

    </com.google.android.material.appbar.AppBarLayout>

    <ImageView
        android:id="@+id/dice_image"
        android:layout_width="275dp"
        android:layout_height="335dp"
        android:contentDescription="@+id/diceImage_imageView_description"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.395"
        app:srcCompat="@drawable/empty_dice" />

    <Button
        android:id="@+id/roll_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/Container_Button_Padding"
        android:text="@string/roll"
        android:textAlignment="center"
        android:textAllCaps="false"
        android:textColor="@color/white"
        android:textSize="25sp"
        app:backgroundTint="@color/bold_button_color"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/dice_image"
        app:layout_constraintVertical_bias="0.32"
        app:rippleColor="@color/button_ripple" />


</androidx.constraintlayout.widget.ConstraintLayout>

这是我针对同一活动的Kotlin代码

package com.example.diceroller

import android.content.Context
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.widget.*
import androidx.appcompat.app.AppCompatDelegate

class MainActivity : AppCompatActivity() {
    lateinit var diceImage: ImageView
    lateinit var sharedPreferences: SharedPreferences
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(findViewById(R.id.activity_toolbar))
        val rollButton: Button = findViewById(R.id.roll_button)
        diceImage = findViewById(R.id.dice_image)
        rollButton.setonClickListener {
            rollDice()
        }

        sharedPreferences = getSharedPreferences(getString(R.string.app_name),Context.MODE_PRIVATE)

        when (sharedPreferences.getString("Night_Mode_State",getString(R.string.follow_system))) {
            getString(R.string.follow_system) -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYstem)
            getString(R.string.light_mode) -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
            getString(R.string.dark_mode) -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
        }

    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        val inflater: MenuInflater = menuInflater
        inflater.inflate(R.menu.actionbar,menu)
        return true
    }

    override fun onoptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.follow_system -> sharedPreferences.edit().putString("Night_Mode_State",getString(R.string.follow_system)).apply()
            R.id.light_mode -> sharedPreferences.edit().putString("Night_Mode_State",getString(R.string.light_mode)).apply()
            R.id.dark_mode -> sharedPreferences.edit().putString("Night_Mode_State",getString(R.string.dark_mode)).apply()
        }
        return true
    }
    private fun rollDice() {
        when ((1..6).random()) {
            1 -> diceImage.setimageResource(R.drawable.dice_1)
            2 -> diceImage.setimageResource(R.drawable.dice_2)
            3 -> diceImage.setimageResource(R.drawable.dice_3)
            4 -> diceImage.setimageResource(R.drawable.dice_4)
            5 -> diceImage.setimageResource(R.drawable.dice_5)
            else -> diceImage.setimageResource(R.drawable.dice_6)
        }
    }
}

这个要点包括我认为正确回答此问题所需的其他文件,即:菜单资源文件,颜色资源文件和样式资源文件https://gist.github.com/sbeve72/36a71919f1f965c5dcd466db1e099b4f.js"

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)