开发者

Not able to access ViewStub'child

开发者 https://www.devze.com 2023-03-05 12:58 出处:网络
I am trying to use VIEWSTUB inside the merge tag.and its working well.I\'m able to catch onclicklistenr of ViewStub\'s pare开发者_开发知识库nt button.But i want to access the button that is inside the

I am trying to use VIEWSTUB inside the merge tag.and its working well.I'm able to catch onclicklistenr of ViewStub's pare开发者_开发知识库nt button.But i want to access the button that is inside the viewstub.

1.Main xml:

<merge>
<LinearLayout>
<Button></Button>
<ViewStub></ViewStub>
</LinearLayout>
</merge>

2.view stub layout

<Button android:id="@+id/button_cancel" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:minWidth="100dip"
android:text="Next" />
<ImageView 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:background="@drawable/golden_gate"
/>
</LinearLayout>

I am inflating view stub in an activity...here i want to fire click event on button cancel.How it will be possible


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消