Android 深色模式下的颜色

Alc*_*fra 12 java android themes android-studio android-darkmode

各位开发者大家好。

我已经使用Android Studio开发了 Android 应用程序,但我只考虑了灯光模式的布局主题。它看起来是这样的:

灯光模式

所以现在,当我将手机切换到深色模式时,会发生这种情况:

深色模式

如何更改深色模式下显示的颜色?

小智 31

res/values/colors
两种颜色
colors.xml
colors.xml(night)


如果 night 不存在,您可以按如下方式创建:
右键单击​​该values文件夹。
下一个New。然后 在字段中输入Values Resource file
并在字段中 输入。colorsfile name
values-nightdirectory name


在colors.xml中,选择灯光的颜色,例如:

<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryVariant">#303F9F</color>
<color name="colorButton">#303F9F</color>
Run Code Online (Sandbox Code Playgroud)

在colors.xml(night)中,设置夜晚的颜色(与颜色同名):

<color name="colorPrimary">#212121</color>
<color name="colorPrimaryVariant">#000000</color> 
<color name="colorButton">#10ffffff</color>
Run Code Online (Sandbox Code Playgroud)

现在,如果在布局中,例如:

<Button
     android: layout_width = "wrap_content"
     android: layout_height = "wrap_content"
     android: background = "@color/colorButton" />
Run Code Online (Sandbox Code Playgroud)

它是根据您在浅色和夜间颜色中放入的内容来选择的。


Blu*_*ell 6

在此输入图像描述

https://developer.android.com/guide/topics/resources/providing-resources

使用文件夹:

/values/
/values-night/
Run Code Online (Sandbox Code Playgroud)

它在这里也提到了: https: //developer.android.com/guide/topics/ui/look-and-feel/darktheme#config-changes

“夜间合格资源”

Your themes and styles should avoid hard-coded colors or icons intended for use under a light theme. You should use theme attributes (preferred) or night-qualified resources instead.
Run Code Online (Sandbox Code Playgroud)