Get root view from current activity

后端 未结 12 2039
不知归路
不知归路 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条回答
  •  旧时难觅i
    2020-11-22 10:03

    if you are in a activity, assume there is only one root view,you can get it like this.

    ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this
            .findViewById(android.R.id.content)).getChildAt(0);
    

    you can then cast it to your real class

    or you could using

    getWindow().getDecorView();
    

    notice this will include the actionbar view, your view is below the actionbar view

提交回复
热议问题