Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ContextThemeWrapperでthemeをより賢く

rmakiyama
April 27, 2020

 ContextThemeWrapperでthemeをより賢く

Android開発において欠かせないthemeをContextThemeWrapperを使ってさらに便利に使っていきましょうという発表です。

サンプルコードも書いてみたのでご興味があれば以下をご覧ください。
https://github.com/rmakiyama/android-theme-study

rmakiyama

April 27, 2020
Tweet

More Decks by rmakiyama

Other Decks in Programming

Transcript

  1. theme """<!-- themes.xml ""--> <style name="AppTheme" parent=“…”> <item name="colorPrimary">@color/app_purple_500"</item> <item

    name="colorPrimaryVariant">@color/app_purple_700"</item> <item name="colorSecondary">@color/app_teal_500"</item> <item name="colorSecondaryVariant">@color/app_teal_700"</item> "</style> UIFNFBUUSJCVUFT
  2. theme attributes """<!-- values/themes.xml ""--> <style name="AppTheme" parent=“…”> <item name=“colorSecondary">@color/app_purple_500"</item>

    … "</style> """<!-- values-night/themes.xml ""--> <style name="AppTheme" parent=“…”> <item name=“colorSecondary">@color/app_purple_200"</item> … "</style>
  3. theme attributes """<!-- values/themes.xml ""--> <style name="AppTheme" parent=“…”> <item name=“colorSecondary">@color/app_purple_500"</item>

    … "</style> """<!-- values-night/themes.xml ""--> <style name="AppTheme" parent=“…”> <item name=“colorSecondary">@color/app_purple_200"</item> … "</style>
  4. ςʔϚΛஔ͖׵͑ͯΈΔ """<!-- themes.xml ""--> <style name=“AppTheme”> <item name="colorPrimary">@color/app_purple_500"</item> … "</style>

    """<!-- fragment_layout.xml ""--> <ImageView … android:background="?attr/colorPrimarySurface" … app:srcCompat="@drawable/ic_launcher_foreground" … "/>
  5. ςʔϚΛஔ͖׵͑ͯΈΔ """<!-- themes.xml ""--> <style name=“AppTheme”> <item name="colorPrimary">@color/app_purple_500"</item> … "</style>

    <style name=“AppTheme.Pro”> <item name="colorPrimary">@color/app_orange_500#</item> … #</style>
  6. ςʔϚΛஔ͖׵͑ͯΈΔ """<!-- ProCaseFragment.kt ""--> class ProCaseFragment : DaggerFragment(R.layout.fragment_pro_case) { @Inject

    lateinit var status: UserStatusSetting override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { val themedInflater: LayoutInflater = if (status.isProMode) { inflater.cloneInContext( ContextThemeWrapper(requireContext(), R.style.AppTheme_Pro) ) } else inflater return super.onCreateView(themedInflater, container, savedInstanceState) } }
  7. ςʔϚΛஔ͖׵͑ͯΈΔ val themedInflater: LayoutInflater = if (status.isProMode) { inflater.cloneInContext( ContextThemeWrapper(requireContext(),

    R.style.AppTheme_Pro) ) } else inflater return super.onCreateView(themedInflater, container, savedInstanceState)
  8. ςʔϚΛஔ͖׵͑ͯΈΔ val themedInflater: LayoutInflater = if (status.isProMode) { inflater.cloneInContext( ContextThemeWrapper(requireContext(),

    R.style.AppTheme_Pro) ) } else inflater return super.onCreateView(themedInflater, container, savedInstanceState)
  9. ςʔϚΛஔ͖׵͑ͯΈΔ val themedInflater: LayoutInflater = if (status.isProMode) { inflater.cloneInContext( ContextThemeWrapper(requireContext(),

    R.style.AppTheme_Pro) ) } else inflater return super.onCreateView(themedInflater, container, savedInstanceState)
  10. ςʔϚΛஔ͖׵͑ͯΈΔ val themedInflater: LayoutInflater = if (status.isProMode) { inflater.cloneInContext( ContextThemeWrapper(requireContext(),

    R.style.AppTheme_Pro) ) } else inflater return super.onCreateView(themedInflater, container, savedInstanceState)
  11. ςʔϚΛஔ͖׵͑ͯΈΔ """<!-- themes.xml ""--> <style name=“AppTheme”> <item name="colorPrimary">@color/app_purple_500"</item> … "</style>

    <style name=“AppTheme.Pro”> <item name="colorPrimary">@color/app_orange_500"</item> … "</style> """<!-- fragment_layout.xml ""--> <ImageView … android:background="?attr/colorPrimarySurface" … app:srcCompat="@drawable/ic_launcher_foreground" … "/>
  12. ςʔϚΛஔ͖׵͑ͯΈΔ """<!-- ProCaseFragment.kt ""--> class ProCaseFragment : DaggerFragment(R.layout.fragment_pro_case) { @Inject

    lateinit var status: UserStatusSetting override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { val themedInflater: LayoutInflater = if (status.isProMode) { inflater.cloneInContext( ContextThemeWrapper(requireContext(), R.style.AppTheme_Pro) ) } else inflater return super.onCreateView(themedInflater, container, savedInstanceState) } }
  13. ςʔϚΛஔ͖׵͑ͯΈΔ """<!-- ProCaseFragment.kt ""--> class ProCaseFragment : DaggerFragment(R.layout.fragment_pro_case) { @Inject

    lateinit var status: UserStatusSetting override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { val themedInflater: LayoutInflater = if (status.isProMode) { inflater.cloneInContext( ContextThemeWrapper(requireContext(), R.style.AppTheme_Pro) ) } else inflater return super.onCreateView(themedInflater, container, savedInstanceState) } }