Not able to access ViewStub'child

亡梦爱人 提交于 2019-12-01 18:09:59

Let's suppose your ViewStub ID is view_stub. You need to do the following in the activity:

ViewStub viewStub = (ViewStub) findViewById(R.id.view_stub);
View inflatedView = viewStub.inflate();
Button button = (Button) inflatedView.findViewById(R.id.button_cancel);

Now you can do whatever you want with the button :) That is, the inflate method returns the stub layout which contains the actual elements from the XML file.

Of course, you can always have the onClick XML attribute...

As for removing the ViewStub - the question is two-fold (check http://developer.android.com/resources/articles/layout-tricks-stubs.html):

  • before inflation of the ViewStub - you cannot actually remove it. There's no need, though, since ViewStub "has no dimension, it does not draw anything and does not participate in the layout in any way".

  • after inflation - you just take the View returned by the ViewStub.inflate() method and do whatever you want with it - for example, hide it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!