Layout controls for android

Source: Internet
Author: User

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.

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.