Get root view from current activity

后端 未结 12 2050
不知归路
不知归路 2020-11-22 09:18

I know how to get the root view with View.getRootView(). I am also able to get the view from a button\'s onClick event where the argument is a View. But how can

12条回答
  •  深忆病人
    2020-11-22 09:57

    Kotlin Extension Solution

    Use this to simplify access in an Activity. Then you can directly refer to rootView from the Activity, or activity.rootView outside of it:

    val Activity.rootView get() = window.decorView.rootView
    

    If you'd like to add the same for Fragments for consistency, add:

    val Fragment.rootView get() = view?.rootView
    

提交回复
热议问题