Get root view from current activity

后端 未结 12 2069
不知归路
不知归路 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 10:17

    Just incase Someone needs an easier way:

    The following code gives a view of the whole activity:

    View v1 = getWindow().getDecorView().getRootView();

    To get a certian view in the activity,for example an imageView inside the activity, simply add the id of that view you want to get:

    View v1 = getWindow().getDecorView().getRootView().findViewById(R.id.imageView1);
    

    Hope this helps somebody

提交回复
热议问题