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