android实现夜间模式_Android中的夜间模式实现

android实现夜间模式

Easiest way to implement night mode in android.

在Android中实现夜间模式的最简单方法。

Android has recently introduced Dark theme from Android 10 which allows user to easily toggle between light/dark theme. Since then many popular applications have been updated to support dark theme. So we in our company too decided to give it a go in our new application.

Android最近从Android 10引入了Dark主题,该主题使用户可以轻松在明/暗主题之间切换。 从那时起,许多流行的应用程序已更新为支持深色主题。 因此,我们公司也决定在我们的新应用程序中使用它。

Follow the article to support dark mode in your application and also to learn how we implemented it in our app.

请按照本文的内容在您的应用程序中支持暗模式,并了解我们如何在应用程序中实现暗模式。

Before starting make sure to update your appcompact to the latest version.

在开始之前,请确保将您的appcompact更新到最新版本。

implementation 'androidx.appcompat:appcompat:1.1.0'

更新主题 (Updating Theme)

To enable dark mode you must implement your theme from DayNight.

要启用暗模式,您必须从DayNight实现主题。

<style name="AppTheme"parent="Theme.AppCompat.DayNight.NoActionBar">

切换主题 (Switching the Theme)

Now to switch the mode just call the method AppCompatDelegate.setDefaultNightMode() which takes any one of the following 4 arguments:

现在要切换模式,只需调用方法AppCompatDelegate.setDefaultNightMode() ,该方法将使用以下4个参数之一:

  • MODE_NIGHT_NO : uses light mode

    MODE_NIGHT_NO:使用灯光模式

  • MODE_NIGHT_YES : uses dark mode

    MODE_NIGHT_YES :使用黑暗模式

  • MODE_NIGHT_FOLLOW_SYSTEM : uses system’s night mode setting to determine if it is night or not

    MODE_NIGHT_FOLLOW_SYSTEM :使用系统的夜间模式设置来确定是否为夜间

  • MODE_NIGHT_AUTO_BATTERY : uses dark mode when system’s battery saver is enabled

    MODE_NIGHT_AUTO_BATTERY:启用系统的省电模式时使用暗模式

AppCompatDeegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)

The above function will enable dark mode in your app. However, it uses default theme of your device. You might want to further customize your application by defining your own attributes for dark mode.

上面的功能将在您的应用中启用暗模式。 但是,它使用设备的默认主题。 您可能希望通过定义黑暗模式的属性来进一步自定义应用程序。

色彩 (Colors)

Create a new colors resource file inside a new android resource directory called values-night. Your folder will look something like this

在名为values-night的新android资源目录中创建一个新的colors资源文件。 您的文件夹将如下所示

Image for post

画图 (Drawables)

Create a separate drawable folder named drawable-night to define images for dark mode.

创建一个名为drawable-night的单独的可绘制文件夹,以定义暗模式下的图像。

Image for post

In our application we have tons of icons and creating new icons specifically for dark mode seemed some what tedious. So I modified the fillColor of the icons to my custom defined iconColor which has defined in both of my colors resources.

在我们的应用程序中,我们有大量的图标,专门为黑暗模式创建新图标似乎有些乏味。 因此,我将图标的fillColor修改为我的自定义定义的iconColor,这两个颜色资源中均已定义。

<path
android:fillColor="@color/iconColor"
.../>

To use dynamic coloring in icons the minSdkVersion must be 21 or greater.

要在图标中使用动态着色, minSdkVersion必须为21或更大。

持续存在 (Persisting)

At this point, you will be able to change your app into dark/light mode. However once you restart your app, it will revert back to the light mode. So in other to display the selected mode, you have to save the user’s choice and update your UI accordingly during the app start up. It is done in onCreate method of the application file.

此时,您将可以将应用程序更改为暗/亮模式。 但是,一旦重新启动应用程序,它将恢复为轻模式。 因此,以其他方式显示所选模式时,您必须保存用户的选择并在应用程序启动期间相应地更新UI。 这是在应用程序文件的onCreate方法中完成的。

Note: You can update dark/light mode from your activity. However updating it in the activity recreates the activity. So it is recommended to update the mode in the application file.

注意:您可以从活动中更新暗/亮模式。 但是,在活动中更新它会重新创建活动。 因此,建议更新应用程序文件中的模式。

还有瞧。 (And Voila..)

You can now enjoy night mode in your application.

您现在可以在应用程序中享受夜间模式。

Image for post

Github sample for the night mode.

夜间模式的Github示例。

https://github.com/RumiRajbhandari/NavigationComponentDemo.git

翻译自: https://medium.com/@rumirajbhandari/night-mode-implementation-in-android-6bdadc00fa24

android实现夜间模式

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值