从父片段设置子片段的主题

时间:2019-08-05 08:55:43

标签: android android-fragments kotlin android-theme

我有一个片段,在那个片段中有三个子片段。我想为存在于父片段中的所有片段应用主题。到目前为止,我在片段上设置主题的唯一方法是在ContextThemeWrapper方法内创建onCreateView并调用cloneInContext(themedContext)以获取主题LayoutInflater。结果很好,但是仅适用于它所在的片段,根本不影响子片段。有什么方法可以一次设置主题并反映每个子片段?

private fun LayoutInflater.withTheme(@StyleRes themeId: Int): LayoutInflater {
    val themedContext = ContextThemeWrapper(this.context, themeId)
    return cloneInContext(themedContext)
}

override fun onCreateView(
    inflater: LayoutInflater, 
    container: ViewGroup?, 
    savedInstanceState: Bundle?
): View? {

    return inflater
          .withTheme(R.style.AppTheme_Red)
          .inflate(R.layout.fragment_products, container, false)
}

或者也许我们最终可能会为每个片段设置主题?

0 个答案:

没有答案
相关问题