从片段更改主题

时间:2020-02-13 02:44:59

标签: android kotlin styles fragment android-architecture-navigation

我有一个无法更改样式主题的问题。我想确保每次导航到不同片段时,应用程序主题都会改变。希望可以帮助我解决这个问题。预先谢谢你。

enter image description here

class MainKonsesiFragment : Fragment() {

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


        //create ContextThemeWrapper from the original Activity Context with the custom theme
        val context = ContextThemeWrapper(activity, R.style.AppKonsesiTheme)
        //clone the inflater using the ContextThemeWrapper
        val localInflater = inflater.cloneInContext(context)

        val binding:FragmentMainKonsesiBinding = DataBindingUtil.inflate(localInflater,R.layout.fragment_main_konsesi,container,false)

        binding.cardInputPemantauan.setOnClickListener(
            Navigation.createNavigateOnClickListener(R.id.action_mainKonsesiFragment_to_inputPemantauanFragment)
        )
        binding.cardListKonsesi.setOnClickListener(
            Navigation.createNavigateOnClickListener(R.id.action_mainKonsesiFragment_to_listKonsesiFragment)
        )
        return binding.root
    }
}

1 个答案:

答案 0 :(得分:0)

你能尝试

 getActivity.getTheme().applyStyle(R.style.AppKonsesiTheme, true);

甚至可以打电话

getActivity().recreate();

如果需要的话。以为您可能需要在创建片段之前更改主题