Android basic entry layout (2)

Source: Internet
Author: User

1. LinearLayout (linear layout ):


LinearLayout is the first layout where only one control can be placed in one row or column. Now let's look at an instance:

Activity_main.xml:

         
          
          
  
 
Running image:



The control is vertically arranged. We can use android: orientation = "vertical" to set LinearLayout to vertically arranged.

Describes common public attributes:

Android: layout_width specifies the width of the control or view group.

Android: layout_height specifies the height of the control or view group.

Android: layout_marginTop: additional space on the top side of the control or view group

Android: layout_marginBottom: specify additional space for the control or view group bottom edge.

Android: layout_marginLeft: extra space on the left of the control or view group

Android: layout_marginRight: extra space on the right of the control or view group


Here, we will explain the meaning of setting properties in layout_width and layout_height:

1). fill_parent:

Setting the layout of a component to fill_parent will forcibly extend the component to fill as much space as possible in the layout unit. This is basically the same as the dockstyle attribute of the Windows Control. Setting a top layout or control to fill_parent will forcibly overwrite the entire screen.

2). match_parent:

In Android2.2, match_parent and fill_parent are the same. The two parameters have the same meaning. match_parent is more appropriate, so both words can be used from 2.2. If you want to use a lower version, you need to use fill_parent.

3). wrap_content:

Setting the size of a view to wrap_content will forcibly extend the view to display all the content, and the layout element will change the size based on the content. Setting the size of a view to wrap_content is basically equivalent to setting the Autosize attribute of a Windows Control to True.


When you change android: orientation = "vertical" in the above layout to android: orientation = "horizontal", this is the horizontal layout:

Example (modify the layout File above ):

         
          
          
  
 

Running image:

We can see from the figure that when horizontal layout is used, the control occupies a column,

Android: background this property sets the background color.

Android: layout_width: when setting this attribute, I didn't use the three property values, but defined by myself. At this time, we need to understand the measurement units of android:

1). dp-density-independent pixels. 1 dp is equivalent to 1 pixel on the screen of 160dpi. We recommend that you use dp as the measurement unit when you specify the size of the control in the layout.

2). sp -- pixels irrelevant to the ratio. Similar to dp, we recommend that you specify the font size.

3). pt -- LB, 1 lb equals 1/72 inch (screen-based physical size ).

4). px -- pixel, which corresponds to the actual size on the screen. We do not recommend this unit because your application will be correctly displayed on devices of different screen sizes.


2. RelativeLayout (relative layout ):


In many cases, linear layout cannot meet our requirements. For example, if we need to display multiple controls in one row, we need RelativeLayout for relative layout, relativeLayout allows child elements to specify their positions relative to other elements or parent elements (specified by ID ).

Common attributes:

Android: layout_above: Move the control's bottom to the control with the given ID

Android: layout_below: place the top of the control under the control with the given ID

Android: layout_toLeftOf align the right edge of the control with the left edge of the control with the given ID

Android: layout_toRightOf align the left edge of the control with the right edge of the control with the given ID

Android: layout_alignRaseline the baseline of the control is aligned with the baseline of the control with the given ID

Android: layout_alignBottom: Compares the bottom edge of the control with the bottom edge of the given ID control.

Android: layout_alignLeft: Align the left edge of the control with the left edge of the given ID control.

Android: layout_alignRight: Align the right edge of the control with the right edge of the given ID Control

Android: layout_alignTop: aligntop

Android: layout_alignParentBottom if the value is true, the bottom of the control is aligned with the bottom of the parent control.

Android: layout_alignParentLeft if the value is true, the left side of the control is aligned with the left side of the parent control.

Android: layout_alignParentRight if the value is true, the right side of the control is aligned with the right side of the parent control.

Android: layout_alignParentTop if the value is true, the top of the control is aligned with the top of the parent control.

Android: layout_centerHorizontal if the value is true, the control is placed in the center of the horizontal direction.

Android: layout_centerInParent if the value is true, the control is placed in the center of the horizontal and vertical directions of the parent Control

Android: layout_centerVertical if the value is true, the control is placed in the center of the vertical direction

Android: layout_marginLeft this is the margin.

Android: layout_marginTop offset value (relative to the above-edge Moving Value xxdp)

Android: layout_marginBottom offset value;

Android: layout_marginLeft value of the left offset;

Android: value of the right offset of layout_marginRight;

Example:

     
      
      
      
      
   
 

Running image:


In the preceding example, linear layout cannot be implemented, but relative layout is easy to implement.


3. TableLayout (form layout ):


TableLayout distributes the positions of child elements to rows or columns. TableLayout consists of multiple TableRow controls.

Instance:

     
          
           
       
      
          
           
       
      
          
           
       
  
 


Running image:


Use Fill. If this is not done, the login button is displayed under Password TextView.


Iv. FrameLayout layout:


FrameLayout is the simplest layout. In this layout, the entire interface is treated as a blank standby area, and all child elements cannot be specified for placement, they are all placed in the upper left corner of the area, and the child elements behind them directly overwrite the child elements above the front to block all the child element parts above the front.

Example:

<frameLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="fill_parent">
 
 
 </frameLayout>

Running image:

The FrameLayout layout constantly stacks controls on the previous one.
5. absoluteLayout (absolute location layout): AbsoluteLayout can be used to specify the exact location of its child elements (android: layout_x and android: layout_y). However, this layout is not recommended, because the positions displayed on the screen at different resolutions are different. This layout is not described too much.





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.