Android dynamic Add button

Source: Internet
Author: User
First, let's take a look at the layouinflater class: instantiate a layout XML file as a view object. Instead of using it directly, he uses getlayoutinflater () or getsystemservice (string) to obtain the standard layoutinflater instance. The instance has connected the context and configured the running device correctly. For example, [Java] view plaincopylayoutinflater Inflater = (layoutinflater) context. getsystemservice (context. layout_inflater_service); Public View inflate (INT resource, viewgroup root) since: API Level 1 inflate a new view hierarchy from the specified XML resource. throws inflateexception if there is an error. parametersresource ID for an XML layout resource to load (e.g ., r. layout. main_page) Root optional vie W to be the parent of the generated hierarchy. returnsthe Root View of the inflated hierarchy. if root was supplied, this is the root view; otherwise it is the root of the inflated XML file. suppose there is such an XML file (guess_button.xml) Put into a buttontablelayout: [HTML] view plaincopy <? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: Orientation = "vertical"> <button Android: id = "@ + ID/newgustmutton" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_weight = "1"/> </linearlayout> (guess_button.xml) [HTML] view plaincopy <tablelayout Android: Id = "@ + ID/buttontablelayout" Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: layout_weight = "1" Android: stretchcolumns = "0, 1, 2"> <tablerow Android: Id = "@ + ID/tablerow0" Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: Orientation = "horizontal"> </tablerow> <tablerow Android: Id = "@ + ID/tablerow1" Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: Orientation = "horizontal"> </tablerow> <tablerow Android: Id = "@ + ID/tablerow2" Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: Orientation = "horizontal"> </tablerow> </tablelayout> (main. the extended buttontablelayout defined in XML is also called buttontablelayout, that is, buttontablelayout = (tablelayout) findviewbyid (R. id. buttonlayout);) Now we can expand its first line: [Java] view plaincopylayoutinflater Inflater = (layoutinflater) context. getsystemservice (context. layout_inflater_service); button newguw.utton = (button) Inflater. inflater (R. layout. guess_button, null); // obtain the button newgustmutton. settext ("ABC"); // set the button text newguw.utton. setonclicklistener (gustmuttonlistener); // sets the listener (tablerow) buttontablelayout. getchildat (1); // Add newgustmutton to the first row of table. OK. The whole process is completed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.