在 AndroidX 中支持深色和浅色主题
Supporting Dark & Light Theme in AndroidX
我目前使用以下父主题 Theme.MaterialComponents.Light.NoActionBar
并且刚刚将我的 material 设计库更新为
implementation 'com.google.android.material:material:1.1.0'
在我的应用程序中弄乱了一些颜色
所以我决定更新它以支持浅色和深色主题。我将 post 我为实现这一目标所做的工作,以节省其他人搜索的时间
进行一些搜索后,这就是我所做的详细信息
将父主题更改为Theme.MaterialComponents.DayNight.NoActionBar
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="android:navigationBarColor">@color/transparent</item>
<item name="android:windowContentTransitions" tools:targetApi="lollipop">true</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorSecondary">@color/colorAccent</item>
</style>
删除所有背景颜色、文本颜色等...必要时
以这种方式添加颜色:"?android:attr/colorBackground"
、"?attr/colorOnBackground"
、"?attr/colorSurface"
在需要的地方
要在代码中更改一些颜色然后使用此函数
fun getColor(context: Context, colorResId: Int): Int {
val typedValue = TypedValue()
val typedArray = context.obtainStyledAttributes(typedValue.data, intArrayOf(colorResId))
val color = typedArray.getColor(0, 0)
typedArray.recycle()
return color
}
示例:
setTextColor(Utils.getColor(context, R.attr.colorError))
- 必要时添加 values-night 目录以支持黑暗和夜间模式下的不同颜色
- 在目录中添加 colors.xml 文件,然后覆盖 colors.xml
中写入的任何颜色
示例:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimaryDark">#1f2021</color>
</resources>
深色主题:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
普通主题:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
oncreate、button click等可以根据需要设置主题
styles.xml values
个文件夹
<resources>
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColorPrimary">@android:color/black </item>
</style>
</resources>
Add Folder to res
folder name values-night
in that add color.xml
and style.xml
to your need.
styles.xml values-night
个文件夹
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/orange</item>
<item name="colorPrimaryDark">@color/orangeDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColorPrimary">@android:color/white</item>
</style>
color.xml
values
个文件夹
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="daynight_textColor">#6cbabb</color>
</resources>
color.xml
values-night
个文件夹
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="daynight_textColor">#ff8222</color>
</resources>
我目前使用以下父主题 Theme.MaterialComponents.Light.NoActionBar
并且刚刚将我的 material 设计库更新为
implementation 'com.google.android.material:material:1.1.0'
在我的应用程序中弄乱了一些颜色
所以我决定更新它以支持浅色和深色主题。我将 post 我为实现这一目标所做的工作,以节省其他人搜索的时间
进行一些搜索后,这就是我所做的详细信息
将父主题更改为Theme.MaterialComponents.DayNight.NoActionBar
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="android:navigationBarColor">@color/transparent</item>
<item name="android:windowContentTransitions" tools:targetApi="lollipop">true</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorSecondary">@color/colorAccent</item>
</style>
删除所有背景颜色、文本颜色等...必要时
以这种方式添加颜色:
"?android:attr/colorBackground"
、"?attr/colorOnBackground"
、"?attr/colorSurface"
在需要的地方
要在代码中更改一些颜色然后使用此函数
fun getColor(context: Context, colorResId: Int): Int {
val typedValue = TypedValue()
val typedArray = context.obtainStyledAttributes(typedValue.data, intArrayOf(colorResId))
val color = typedArray.getColor(0, 0)
typedArray.recycle()
return color
}
示例:
setTextColor(Utils.getColor(context, R.attr.colorError))
- 必要时添加 values-night 目录以支持黑暗和夜间模式下的不同颜色
- 在目录中添加 colors.xml 文件,然后覆盖 colors.xml 中写入的任何颜色
示例:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimaryDark">#1f2021</color>
</resources>
深色主题:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
普通主题:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
oncreate、button click等可以根据需要设置主题
styles.xml values
个文件夹
<resources>
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColorPrimary">@android:color/black </item>
</style>
</resources>
Add Folder to res
folder name values-night
in that add color.xml
and style.xml
to your need.
styles.xml values-night
个文件夹
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/orange</item>
<item name="colorPrimaryDark">@color/orangeDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColorPrimary">@android:color/white</item>
</style>
color.xml
values
个文件夹
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="daynight_textColor">#6cbabb</color>
</resources>
color.xml
values-night
个文件夹
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="daynight_textColor">#ff8222</color>
</resources>