问题
I currently use the following parent theme Theme.MaterialComponents.Light.NoActionBar
and just updated my material design library to
implementation 'com.google.android.material:material:1.1.0'
which messed up some colors in my app
So i am deciding to update it to support both light and dark themes. I will post what i did to achieve this to save some time for others searching
回答1:
After performing some searches, this is what i did in details
Change the parent theme to 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>
Remove all background colors, text colors etc... where its necessary
add colors this way:
"?android:attr/colorBackground"
,"?attr/colorOnBackground"
,"?attr/colorSurface"
where needed
To change some colors in-code then use this function
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
}
Example:
setTextColor(Utils.getColor(context, R.attr.colorError))
- Add values-night directory if necessary to support different colors in dark and night mode
- Add colors.xml file inside the directory and then just override any color written in colors.xml
Example:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimaryDark">#1f2021</color>
</resources>
回答2:
For Dark Theme:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
For Normal Theme:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
You can set theme according to your needs in oncreate and button click etc..
styles.xml of values
folder
<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 of values-night
folder
<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
of values
folder
<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
of values-night
folder
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="daynight_textColor">#ff8222</color>
</resources>
来源:https://stackoverflow.com/questions/60093737/supporting-dark-light-theme-in-androidx