jaredrummler / Cyanea

A theme engine for Android
Apache License 2.0
1.45k stars 144 forks source link

Unable to override color attributes #30

Open tcqq opened 5 years ago

tcqq commented 5 years ago

I want to overwrite the textColorPrimary color to red. When I change the color, the textColorPrimary in the Android Studio preview is red (the correct effect), but the effect of running the display is black, and the color of textColorPrimary has not changed. This problem also exists in the Colorful library. How can I fix this problem?

49334689-a7385380-f616-11e8-8578-feaafdab21b0 image 49334694-c505b880-f616-11e8-9796-b9b8c44beacc tim 20181202094843

jaredrummler commented 5 years ago

If the theme has been modified, Cyanea will call setTheme with a Cyanea based style in Activity#onCreate. Unfortunately this will modify current styles that you might have set.

I will need to come up with a better alternative, but for now, you will have to create 4 styles and override getThemeResId in your activity. This way, you can specify which style Cyanea should use.

Example:

In res/values/styles.xml:

<style name="AppTheme.Light.DarkActionBar" parent="Theme.Cyanea.Light.DarkActionBar">
  <item name="android:textColorPrimary">@color/my_primary_text_color</item>
</style>

<style name="AppTheme.Light" parent="Theme.Cyanea.Light">
  <item name="android:textColorPrimary">@color/my_primary_text_color</item>
</style>

<style name="AppTheme.Dark" parent="Theme.Cyanea.Dark">
  <item name="android:textColorPrimary">@color/my_primary_text_color</item>
</style>

<style name="AppTheme.Dark.LightActionBar" parent="Theme.Cyanea.Dark.LightActionBar">
  <item name="android:textColorPrimary">@color/my_primary_text_color</item>
</style>

Then, in your activity:

class MainActivity : CyaneaAppCompatActivity() {

  /**
   * Get the correct style for Cyanea
   */
  @get:StyleRes
  private val actionBarTheme: Int
    get() = when (cyanea.baseTheme) {
      DARK ->
        if (cyanea.isActionBarLight)
          R.style.AppTheme_Dark_LightActionBar
        else
          R.style.AppTheme_Dark
      LIGHT ->
        if (cyanea.isActionBarDark)
          R.style.AppTheme_Light_DarkActionBar
        else
          R.style.AppTheme_Light
    }

  override fun getThemeResId(): Int = actionBarTheme

}