1. frameLayout: all controls are stacked Based on the screen Origin
The key code is as follows:
Super. onCreate (savedInstanceState); FrameLayout layout = new FrameLayout (this); // defines the frame layout manager FrameLayout. layoutParams layoutParam = new FrameLayout. layoutParams (ViewGroup. layoutParams. FILL_PARENT, ViewGroup. layoutParams. FILL_PARENT); // defines the layout manager parameter FrameLayout. layoutParams viewParam = new FrameLayout. layoutParams (ViewGroup. layoutParams. WRAP_CONTENT, ViewGroup. layoutParams. WRAP_CONTENT); // define the parameter ImageView img = new ImageView (this) of the display component; // define the image component img. setImageResource (R. drawable. radio); // define the displayed image EditText edit = new EditText (this); // define the text input component edit. setText ("enter your name... "); // set the displayed text Button but = new Button (this); // define the Button. setText ("click"); // set the text layout of the button. addView (img, viewParam); // Add the component layout. addView (edit, viewParam); // Add the component layout. addView (but, viewParam); // Add component super. setContentView (layout, layoutParam); // Add a layout manager to the screen
2. linnerLayout: Linear layout: horizontal and vertical use
Controls use two methods: 1. dynamically add controls; 2. xml configuration file.
The key code for dynamic creation is as follows:
Super. onCreate (savedInstanceState); LinearLayout layout = new LinearLayout (this); // defines the linear layout manager LinearLayout. layoutParams params = new LinearLayout. layoutParams (ViewGroup. layoutParams. FILL_PARENT, ViewGroup. layoutParams. FILL_PARENT); // defines the layout manager parameter layout. setOrientation (LinearLayout. VERTICAL); // defines the layout manager arrangement method // defines the component layout LinearLayout. layoutParams txtparams = new LinearLayout. layoutParams (ViewGroup. layoutParams. FILL_PARENT, ViewGroup. layoutParams. WRAP_CONTENT); // defines the layout manager parameter TextView txt = new TextView (this); // defines the text display txt. setText ("MIDP Software Institute"); // set text txt. setTextColor (this. getResources (). getColor (R. color. red); // set the text color txt. setTextSize (25); // set the font size of Text Information layout. addView (txt, txtparams); // Add the setContentView (layout, params) component. // Add a new layout manager.
3. Relative layout of relativeLayout: Create a control relative to a certain control on an existing control.
The key code is as follows:
Super. onCreate (savedInstanceState); super. setContentView (R. layout. main); // read the existing layout manager RelativeLayout rl = (RelativeLayout) super. findViewById (R. id. mylayout); // locate the layout manager RelativeLayout. layoutParams param = new RelativeLayout. layoutParams (ViewGroup. layoutParams. FILL_PARENT, ViewGroup. layoutParams. WRAP_CONTENT); // defines the param parameter. addRule (RelativeLayout. BELOW, R. id. mybut); // place the new component under mybut. addRule (RelativeLayout. RIGHT_OF, R. id. myimage1); // put it on the right of the first image EditText text = new EditText (this); // enter the text rl. addView (text, param); // adds a component to a layout manager.