ContextThemeWrapperでthemeをより賢く

 ContextThemeWrapperでthemeをより賢く

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

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

E6d26a51159a7863cac28e9d12ccd389?s=128

rmakiyama

April 27, 2020
Tweet

Transcript

  1. 4.
  2. 6.

    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
  3. 8.

    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. 9.

    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>
  5. 17.

    ςʔϚΛஔ͖׵͑ͯΈΔ """<!-- 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" … "/>
  6. 18.

    ςʔϚΛஔ͖׵͑ͯΈΔ """<!-- 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>
  7. 19.

    ςʔϚΛஔ͖׵͑ͯΈΔ """<!-- 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) } }
  8. 20.

    ςʔϚΛஔ͖׵͑ͯΈΔ val themedInflater: LayoutInflater = if (status.isProMode) { inflater.cloneInContext( ContextThemeWrapper(requireContext(),

    R.style.AppTheme_Pro) ) } else inflater return super.onCreateView(themedInflater, container, savedInstanceState)
  9. 21.

    ςʔϚΛஔ͖׵͑ͯΈΔ val themedInflater: LayoutInflater = if (status.isProMode) { inflater.cloneInContext( ContextThemeWrapper(requireContext(),

    R.style.AppTheme_Pro) ) } else inflater return super.onCreateView(themedInflater, container, savedInstanceState)
  10. 22.

    ςʔϚΛஔ͖׵͑ͯΈΔ val themedInflater: LayoutInflater = if (status.isProMode) { inflater.cloneInContext( ContextThemeWrapper(requireContext(),

    R.style.AppTheme_Pro) ) } else inflater return super.onCreateView(themedInflater, container, savedInstanceState)
  11. 23.

    ςʔϚΛஔ͖׵͑ͯΈΔ val themedInflater: LayoutInflater = if (status.isProMode) { inflater.cloneInContext( ContextThemeWrapper(requireContext(),

    R.style.AppTheme_Pro) ) } else inflater return super.onCreateView(themedInflater, container, savedInstanceState)
  12. 24.

    ςʔϚΛஔ͖׵͑ͯΈΔ """<!-- 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" … "/>
  13. 25.

    ςʔϚΛஔ͖׵͑ͯΈΔ """<!-- 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) } }
  14. 26.

    ςʔϚΛஔ͖׵͑ͯΈΔ """<!-- 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) } }