Use of viewgroup and other container controls in Android

Source: Internet
Author: User

 

In Android, you can customize classes and inherit from container classes such as viewgroup to display the layout you need.

 

If you add a control to viewgroup but it cannot be displayed, the following example can be used for reference.

 

(Mainly to implement the onlayout () method. In this method, perform measure () for each sub-control and then layout it .)

 

Package COM. arui; <br/> Import android. content. context; <br/> Import android. util. log; <br/> Import android. view. view; <br/> Import android. view. viewgroup; <br/> Import android. widget. button; <br/>/** <br/> * example for using viewgroup. <br/> * @ author http://blog.csdn.net/arui319 <br/> * @ version 2010/09/07 <br/> */<br/> public class myviewgroup extends viewgroup {<br/> Public myviewgroup (context) {<br/> super (context); <br/> This. initothercomponent (context); <br/>}< br/> private void initothercomponent (context) {<br/> button abtn = new button (context ); <br/> // Set ID 1 <br/> abtn. setid (1); <br/> abtn. settext ("a btn"); <br/> This. addview (abtn); <br/> button bbtn = new button (context); <br/> // Set ID 2 <br/> bbtn. setid (2); <br/> bbtn. settext ("B BTN"); <br/> This. addview (bbtn); <br/>}< br/> @ override <br/> protected void onlayout (Boolean changed, int L, int T, int R, int B) {<br/> int childcount = getchildcount (); <br/> for (INT I = 0; I <childcount; I ++) {<br/> View child = getchildat (I); <br/> switch (child. GETID () {<br/> case 1: <br/> // 1 is abtn <br/> log. D ("myviewgroup", "btn1 setting"); <br/> child. setvisibility (view. visible); <br/> child. measure (R-L, B-T); <br/> child. layout (0, 0, child. getmeasuredwidth (), Child <br/>. getmeasuredheight (); <br/> break; <br/> case 2: <br/> // 2 is bbtn <br/> log. D ("myviewgroup", "btn2 setting"); <br/> child. setvisibility (view. visible); <br/> child. measure (R-L, B-T); <br/> child. layout (0, 50, child. getmeasuredwidth (), Child <br/>. getmeasuredheight () + 50); <br/> break; <br/> default: <br/> // <br/>}< br/> 

 

Related Article

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.