Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当我点击按钮时,如何在我的android应用程序中添加暗模式_Java_Android - Fatal编程技术网

Java 当我点击按钮时,如何在我的android应用程序中添加暗模式

Java 当我点击按钮时,如何在我的android应用程序中添加暗模式,java,android,Java,Android,我正在创建一个Android应用程序,但我不知道如何将我的应用程序转换为夜间模式,我希望当我点击按钮时,它将切换到夜间模式,当我再次按下它时,它将恢复正常 非常感谢大家。首先在style.xml中创建两个名称不同的样式 <style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar"> <item name="colorPrimary">@color/colorPri

我正在创建一个Android应用程序,但我不知道如何将我的应用程序转换为夜间模式,我希望当我点击按钮时,它将切换到夜间模式,当我再次按下它时,它将恢复正常


非常感谢大家。

首先在style.xml中创建两个名称不同的样式

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">#fff</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="PrimaryTextColor">#fff</item>

    </style>

    <style name="AppThemeDark" parent="Theme.MaterialComponents.NoActionBar">
        <item name="colorPrimary">#2196F3</item>
        <item name="colorPrimaryDark">#303030</item>
        <item name="colorAccent">#03A9F4</item>
        <item name="PrimaryTextColor">#03A9F4</item>

    </style>
最后是改变主题 黑暗的

照明

setTheme(R.style.Light)
recreate()
更新: 将此样式放在values-v19.xml和values-v21.xml style.xml文件中

<style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowTranslucentStatus">false</item>
    </style>

    <style name="AppThemeDark.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowTranslucentStatus">false</item>
    </style>

首先在style.xml中创建两个名称不同的样式

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">#fff</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="PrimaryTextColor">#fff</item>

    </style>

    <style name="AppThemeDark" parent="Theme.MaterialComponents.NoActionBar">
        <item name="colorPrimary">#2196F3</item>
        <item name="colorPrimaryDark">#303030</item>
        <item name="colorAccent">#03A9F4</item>
        <item name="PrimaryTextColor">#03A9F4</item>

    </style>
最后是改变主题 黑暗的

照明

setTheme(R.style.Light)
recreate()
更新: 将此样式放在values-v19.xml和values-v21.xml style.xml文件中

<style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowTranslucentStatus">false</item>
    </style>

    <style name="AppThemeDark.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowTranslucentStatus">false</item>
    </style>
<style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowTranslucentStatus">false</item>
    </style>

    <style name="AppThemeDark.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowTranslucentStatus">false</item>
    </style>
android:theme="@style/AppTheme.NoActionBar"