Learning Android custom View (1)

Source: Internet
Author: User

MainActivity is as follows:

Package cc. testviewstudy1; import android. OS. bundle; import android. app. activity; import android. view. layoutInflater; import android. view. view; import android. view. viewParent; import android. widget. relativeLayout;/*** Demo Description: * learning about custom View (1) ** learning materials: * http://blog.csdn.net/guolin_blog/article/details/12921889 * Thank you very much **/public class MainActivity extends Activity {private RelativeLayou T mRelativeLayout; private LayoutInflater mLayoutInflater; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); init (); // test1 (); test2 (); test3 () ;}// each Activity is composed of two parts. // 1 title // 2 contentView // we can set them private void init () {setTitle ("This is title"); setContentView (R. layout. main);} // adding a Button in the View using the button_layout_wrong layout is not accurate enough. because at this time, we cannot use android: layout _ Width and android: layout_height // specify the width and height of the Button. In the end, the Button only displays the size of wrap_content. private void test1 () {mRelativeLayout = (RelativeLayout) findViewById (R. id. relativeLayout); mLayoutInflater = LayoutInflater. from (MainActivity. this); View buttonView = mLayoutInflater. inflate (R. layout. button_layout_wrong, null); mRelativeLayout. addView (buttonView);} // how to solve the problem in test1? // The key lies in the understanding of android: layout_width and android: layout_height // it refers to the width and height of the control in the layout, so android: layout_width and android: layout_height // instead of android: width and android: height. // Therefore, we need to put the control in a layout first, and then specify the width and height for the control. this will have an effect. // private void test2 () {mRelativeLayout = (RelativeLayout) findViewById (R. id. relativeLayout); mLayoutInflater = LayoutInflater. from (MainActivity. this); View buttonView = mLayoutInflater. inflate (R. Layout. button_layout_right, null); mRelativeLayout. addView (buttonView);} // continue with the preceding example: // during each layout, the layout of the outermost layer is not allowed through android: layout_width and android: layout_height // specify the layout width and height, and then setContentView () will it be displayed on the screen? // Why can I specify the width and height of the largest View without nesting a layer of layout? // Isn't this a conflict with the above example? // Actually, it is not. // when loading xml for each layout file. whatever the root layout, FrameLayout is nested in the layout. // This is consistent with the above example. private void test3 () {mRelativeLayout = (RelativeLayout) findViewById (R. id. relativeLayout); ViewParent viewParent = mRelativeLayout. getParent (); System. out. println ("the outermost layer of each layout file is:" + viewParent );}}


Main. xml is as follows:

     
  
 


Button_layout_wrong.xml is as follows:

 

Button_layout_right.xml is as follows:

     
      
 


Zookeeper

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.