Share
Embed
ContextThemeWrapperでthemeをより賢く
Android開発において欠かせないthemeをContextThemeWrapperを使ってさらに便利に使っていきましょうという発表です。
サンプルコードも書いてみたのでご興味があれば以下をご覧ください。
https://github.com/rmakiyama/android-theme-study
rmakiyama
April 27, 2020
Transcript
28 slides
-
theme """<!-- AndroidManifest.xml ""--> <application … android:theme="@style/AppTheme"> <activity … android:label=“@string/app_name"
android:theme=“@style/AppTheme.Hoge”> … "</application> -
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 -
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> -
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> -
ςʔϚΛஔ͖͑ͯΈΔ """<!-- 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" … "/> -
ςʔϚΛஔ͖͑ͯΈΔ """<!-- 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> -
ςʔϚΛஔ͖͑ͯΈΔ """<!-- 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) } } -
ςʔϚΛஔ͖͑ͯΈΔ val themedInflater: LayoutInflater = if (status.isProMode) { inflater.cloneInContext( ContextThemeWrapper(requireContext(),
R.style.AppTheme_Pro) ) } else inflater return super.onCreateView(themedInflater, container, savedInstanceState) -
ςʔϚΛஔ͖͑ͯΈΔ val themedInflater: LayoutInflater = if (status.isProMode) { inflater.cloneInContext( ContextThemeWrapper(requireContext(),
R.style.AppTheme_Pro) ) } else inflater return super.onCreateView(themedInflater, container, savedInstanceState) -
ςʔϚΛஔ͖͑ͯΈΔ val themedInflater: LayoutInflater = if (status.isProMode) { inflater.cloneInContext( ContextThemeWrapper(requireContext(),
R.style.AppTheme_Pro) ) } else inflater return super.onCreateView(themedInflater, container, savedInstanceState) -
ςʔϚΛஔ͖͑ͯΈΔ val themedInflater: LayoutInflater = if (status.isProMode) { inflater.cloneInContext( ContextThemeWrapper(requireContext(),
R.style.AppTheme_Pro) ) } else inflater return super.onCreateView(themedInflater, container, savedInstanceState) -
ςʔϚΛஔ͖͑ͯΈΔ """<!-- 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" … "/> -
ςʔϚΛஔ͖͑ͯΈΔ """<!-- 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) } } -
ςʔϚΛஔ͖͑ͯΈΔ """<!-- 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) } } -
appendix • https:"//github.com/rmakiyama/android-theme-study • https:"//mixi-inc.github.io/AndroidTraining/introductions/1.04.basic- knowledge.html#context%E3%81%A8%E3%81%AF%EF%BC%9F • https:"//developer.android.com/reference/android/view/ContextThemeWrapper • https:"//developer.android.com/guide/topics/ui/themes
• https:"//speakerdeck.com/nickbutcher/developing-themes-with- style-7d0571e3-6008-4ec5-99c6-aa5669520124