Android開発において欠かせないthemeをContextThemeWrapperを使ってさらに便利に使っていきましょうという発表です。
サンプルコードも書いてみたのでご興味があれば以下をご覧ください。 https://github.com/rmakiyama/android-theme-study
ContextThemeWrapperͰthemeΛΑΓݡ͘potatotips #692020/04/27 rmakiyama
View Slide
ɹɹࣗݾհ•ࢁྎ•Radiotalkגࣜձࣾ•AndroidΤϯδχΞ• @_rmakiyama• rmakiyama
theme×ContextThemeWrapper
theme
theme"""…android:theme="@style/AppTheme">…android:label=“@string/app_name"android:theme=“@style/AppTheme.Hoge”>…"
theme"""<br/><br/><item name="colorPrimary">@color/app_purple_500"</item><br/><br/><item name="colorPrimaryVariant">@color/app_purple_700"</item><br/><br/><item name="colorSecondary">@color/app_teal_500"</item><br/><br/><item name="colorSecondaryVariant">@color/app_teal_700"</item><br/><br/>"UIFNFBUUSJCVUFT
theme attributesandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="?attr/colorPrimary"android:text="@string/button_save"android:textColor="?attr/colorOnPrimary" "/>
theme attributes"""<br/><br/><item name=“colorSecondary">@color/app_purple_500"</item><br/><br/>…<br/><br/>""""<br/><br/><item name=“colorSecondary">@color/app_purple_200"</item><br/><br/>…<br/><br/>"
theme attributes tipsandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background=“@color/app_purple_500“android:text="@string/button_save"android:textColor="@android:color/white" "/>
theme attributes tipsandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background=“?attr/colorPrimary“android:text="@string/button_save"android:textColor=“?attr/colorOnPrimary” "/>
ContextThemeWrapper
ContextThemeWrapperjava.lang.Object↳ android.content.Context↳ android.content.ContextWrapper↳ android.view.ContextThemeWrapper↳ android.app.Activity
ContextThemeWrapperͱl"DPOUFYUXSBQQFSUIBUBMMPXTZPVUPNPEJGZPSSFQMBDFUIFUIFNFPGUIFXSBQQFEDPOUFYUlSFGIUUQTEFWFMPQFSBOESPJEDPNSFGFSFODFBOESPJEWJFX$POUFYU5IFNF8SBQQFS
ContextThemeWrapperͱςʔϚΛஔ͖͑ΒΕΔΑʂ
ςʔϚΛஔ͖͑ͯΈΔϓϩεςʔλεʹͳͬͨΒ*NBHF7JFXͷ CBDLHSPVOEͷ৭Λม͑Δ
ςʔϚΛஔ͖͑ͯΈΔ"""<br/><br/><item name="colorPrimary">@color/app_purple_500"</item><br/><br/>…<br/><br/>""""…android:background="?attr/colorPrimarySurface"…app:srcCompat="@drawable/ic_launcher_foreground"… "/>
ςʔϚΛஔ͖͑ͯΈΔ"""<br/><br/><item name="colorPrimary">@color/app_purple_500"</item><br/><br/>…<br/><br/>"<br/><br/><item name="colorPrimary">@color/app_orange_500#</item><br/><br/>…<br/><br/>#
ςʔϚΛஔ͖͑ͯΈΔ"""class ProCaseFragment : DaggerFragment(R.layout.fragment_pro_case) {@Inject lateinit var status: UserStatusSettingoverride fun onCreateView(inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?): View? {val themedInflater: LayoutInflater = if (status.isProMode) {inflater.cloneInContext(ContextThemeWrapper(requireContext(), R.style.AppTheme_Pro))} else inflaterreturn super.onCreateView(themedInflater, container, savedInstanceState)}}
ςʔϚΛஔ͖͑ͯΈΔval themedInflater: LayoutInflater = if (status.isProMode) {inflater.cloneInContext(ContextThemeWrapper(requireContext(), R.style.AppTheme_Pro))} else inflaterreturn super.onCreateView(themedInflater, container, savedInstanceState)
ςʔϚΛஔ͖͑ͯΈΔ"""<br/><br/><item name="colorPrimary">@color/app_purple_500"</item><br/><br/>…<br/><br/>"<br/><br/><item name="colorPrimary">@color/app_orange_500"</item><br/><br/>…<br/><br/>""""…android:background="?attr/colorPrimarySurface"…app:srcCompat="@drawable/ic_launcher_foreground"… "/>
·ͱΊ• theme attributesΛ׆༻͠Α͏• ContextThemeWrapperΛ׆༻͠Α͏
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