java - 如何进行深色模式设置

标签 java android android-fragments android-activity android-dark-theme

我是 android 开发的新手,我希望在我的应用程序中进行暗模式设置。 为此,我创建了一个“preferences.xml”文件,该文件使用“SettingsActivity.java”文件和“SettingsFragment.java”文件显示。我在互联网上寻找了很多解决方案,但都没有用。有没有人有教程、示例或解决方案? 谢谢

首选项.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <ListPreference
            android:key="@string/key_app_theme"
            android:title="@string/app_theme"
            android:summary=""
            android:icon="@drawable/ic_app_theme"
            android:entries="@array/pref_app_theme_entries"
            android:entryValues="@array/pref_app_theme_values"
            android:dialogTitle="@string/app_theme"
            android:defaultValue="1"/>

</PreferenceScreen>

数组.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    
    <string-array name="pref_app_theme_entries">
        <item>@string/system</item>
        <item>@string/battery_saver</item>
        <item>@string/light</item>
        <item>@string/dark</item>
    </string-array>
    <string-array name="pref_app_theme_values">
        <item>1</item>
        <item>2</item>
        <item>3</item>
        <item>4</item>
    </string-array>
    
</resources>

activity_settings.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_settings"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    tools:context=".SettingsActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/activity_settings_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="4dp"
        android:background="@color/colorPrimaryDark"
        app:title="@string/settings"
        app:titleTextColor="@color/colorAccent" />

    <FrameLayout
        android:id="@+id/activity_settings_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/activity_settings_toolbar" />

</RelativeLayout>

SettingsActivity.java

public class SettingsActivity extends AppCompatActivity {

    private static Preference settings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        if (findViewById(R.id.activity_settings_fragment_container) !=null) {
            if (savedInstanceState != null)
                return;

            getFragmentManager().beginTransaction().add(R.id.activity_settings_fragment_container, new SettingsFragment()).commit();
        }

        Toolbar toolbar = findViewById(R.id.activity_settings_toolbar);
        toolbar.setTitleTextAppearance(this, R.style.KaushanScriptRegular);
        toolbar.setNavigationIcon(R.drawable.ic_back);
        toolbar.setNavigationOnClickListener(view -> {
            Intent intent = new Intent(SettingsActivity.this, MainActivity.class);
            startActivity(intent);
            overridePendingTransition(R.anim.swipe_right_enter, R.anim.swipe_right_exit);
        });
    }
}

SettingsFragment.java

public class SettingsFragment extends PreferenceFragment {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.preferences);
}

最佳答案

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.night_mode) {
            // Get the night mode state of the app.
            int nightMode = AppCompatDelegate.getDefaultNightMode();
            //Set the theme mode for the restarted activity
            if (nightMode == AppCompatDelegate.MODE_NIGHT_YES) {
                AppCompatDelegate.setDefaultNightMode
                        (AppCompatDelegate.MODE_NIGHT_NO);
            } else {
                AppCompatDelegate.setDefaultNightMode
                        (AppCompatDelegate.MODE_NIGHT_YES);
            }
// Recreate activity for the theme change to take effect.
            recreate();

        }
        return super.onOptionsItemSelected(item);
    }

此外,您可以使用 Check Theme Style

关于java - 如何进行深色模式设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63365949/

相关文章:

Java:项目中的两个 jar 具有相同的类。

android - 在 android 的 gridview 中插入位图数组而不是可绘制对象

java - 无法启动应用程序 - java.lang.NullPointerException

android - Textview 在第一次点击时为空,但在第二次点击时更新

android - 饼图未显示在 fragment 中(Android)

java - 数据库在导出的 apk 中不起作用?

java - 重新抛出异常时出错

java - @BeforeClass 与静态{}

java - Exoplayer 不在本地存储上播放本地文件

android - Gradle 中的多个 Maven 存储库