Basics of learning Android from 0 (3)-layout manager of view Components

Source: Internet
Author: User
Tags xml attribute

Basics of learning Android from 0 (3)-layout manager of view Components
Android layout managerAndroid ActivityThe component binds the display interface for the activity through setContentView (xml resId, however, in order to better manage the various controls in the user interface of the Android Application (a series of components such as button text box editing and box image), Android provides us with a layout manager. By using the layout manager, the graphic interface of Android is unrelated to the java code, so that the code is separated from the layout, thus reducing the amount of code, the Code only requires some interface component control and some data processing, which is a bit similar to the MVC (modle view control) Structure in java. The layout manager in android plays the role of View, java code plays the role of control, and data storage in android plays the role of modle. After introducing what the layout manager is, next we will introduce the layout manager categories, including: LinearLayout relative layout (RelativeLayout) grid layout (GridLayout) frameLayout (AbsoluteLayout) describes the functions of various layout managers.LinearLayout)First, go to the official documentation to see the introduction of the official documentation (you must read the official documentation). The following official means: arrange its sub-layout in a single column or line. You can call setorientation () to set the direction of the row. You can also specify the arrangement mode. You can set LinearLayout. layoutparams to specify all the sub-elements in the arrangement. Call setgravity () or specify the child weight to fill the layout of the remaining space. The default direction is horizontal. Another important thing is that the components in LinearLayout are arranged in a direction that exceeds the screen width, and the more components are not displayed. Let's take a look at the XML attributes and related methods of LinearLayout, as shown in the following table: the official document will introduce the attributes of the official document in detail: This attribute is set to false, the layout manager is blocked from alignment with its child elements. This is especially useful when its sub-elements use different weights. The default value is true. It must be a Boolean value, which is true or false. When the vertical layout is set, the split between the two buttons sets the alignment of components in the layout manager, this attribute supports top, bottom, left, right, center_vertical, fill_vertical, numeric, fill_horizontal, center, fill, clip_vertical, and clip_horizontal attributes. You can also specify multiple combinations. For example: left | center_vertical indicates that it appears on the left of the screen and is vertically centered. When this attribute is set to true, all child elements with weight have the minimum size of the Maximum child element. You can set the arrangement of components in the layout manager to either horizontal or vertical, vertical by default; in the XML of LinearLayout, for example: android: weightSu M = "5" indicates that the LinearLayout is divided into five areas in total. Then, the controls in LinearLayout use android: layout_wetght = "1 ", this indicates it occupies 1/5 of the layout. After introducing the above LinearLayout attributes, we will use the following examples to demonstrate the use of these attributes: Through the above examples, you can just understand the effects of the attributes of LinearLayout. 2. The relative layout (RelativeLayout) introduces the method through the official Linearlayout document. You can go to the Android official website to view the method and follow the steps above to learn other components. The following layout managers will not be explained in the above way, but will be introduced to you in a brief and clear way. The position of the child components inside the relative layout container is always determined by the sibling component and parent container. Therefore, this layout method is called relative layout, for example: if the position of component B is determined by the position of component A, you must first define component B and determine the position of component B before defining component. Description of XML attributes and related methods of RelativeLayout

XML attributes

Java method

Description

Android: gravity

SetGravity (int)

Sets the alignment of child components in the layout container.

Android: ignoreGravity

SetIgnoreGravity (int)

Set which component does not accept gravity attribute impact

To control the distribution of child components in the layout container, Relative also provides an internal class: RelativeLayout. layoutParams, which provides a large number of XML attributes to control the layout distribution of child components in the Relative layout container. Its xml attributes are shown in the following table:

The values below can only be true or false.

Android: layout_centerHorizontal

Determines whether the child widget is horizontally centered in the layout container.

Android: layout_centerVertical

Determines whether the child widget is in the vertical center of the layout container.

Android: layout_centerInparent

Determines whether the child widget is located in the center of the layout container.

Android: layout_alignParentBottom

Determines whether the child widget is aligned at the bottom of the layout container.

Android: layout_alignParentLeft

Determines whether the child widget is aligned to the left of the layout container.

Android: layout_alignParentRight

Determines whether the child widget is aligned to the right of the layout container.

Android: layout_alignParentTop

Determines whether the child widget is aligned at the top of the layout container.

The following attribute values can only be the id values of other UI components.

Android: layout_toRightOf

Control the sub-component to the right of the id component

Android: layout_toLeftOf

Control the sub-component to the left of the id component

Android: layout_abve

Control the sub-component to be located above the id component given

Android: layout_below

Control the sub-component to be located below the given id component

Android: layout_alignTop

Controls the child widget to be located at the upper boundary alignment of the given id widget.

Android: layout_alignBottom

Controls the sub-component to be located at the bottom boundary alignment of the given id component.

Android: layout_alignLeft

Controls that the child widget is located at the left boundary alignment of the id widget.

Android: layout_alignRight

Controls that the child widget is located at the right boundary alignment of the id widget.

 

The following uses a snowflake effect to show the specific effect of Relative.
<!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E--><relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000">    <!--{cke_protected}{C}%3C!%2D%2D%E5%AE%9A%E4%B9%89%E8%AF%A5%E7%BB%84%E4%BB%B6%E4%BD%8D%E4%BA%8E%E7%88%B6%E5%AE%B9%E5%99%A8%E7%9A%84%E4%B8%AD%E5%A4%AE%2D%2D%3E-->    <textview android:id="@+id/xuehua_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true" android:background="@drawable/iconfont_xuehua">    <!--{cke_protected}{C}%3C!%2D%2D%E5%AE%9A%E4%B9%89%E8%AF%A5%E7%BB%84%E4%BB%B6%E4%BD%8D%E4%BA%8Eview1%E7%9A%84%E4%B8%8A%E6%96%B9%2D%2D%3E-->    <textview android:id="@+id/xuehua_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/xuehua_1" android:layout_alignleft="@id/xuehua_1" android:background="@drawable/iconfont_xuehua">    <!--{cke_protected}{C}%3C!%2D%2D%E5%AE%9A%E4%B9%89%E8%AF%A5%E7%BB%84%E4%BB%B6%E4%BD%8D%E4%BA%8Eview1%E7%9A%84%E4%B8%8B%E6%96%B9%2D%2D%3E-->    <textview android:id="@+id/xuehua_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@id/xuehua_1" android:layout_below="@id/xuehua_1" android:background="@drawable/iconfont_xuehua">    <!--{cke_protected}{C}%3C!%2D%2D%E5%AE%9A%E4%B9%89%E8%AF%A5%E7%BB%84%E4%BB%B6%E4%BD%8D%E4%BA%8Eview1%E7%9A%84%E5%B7%A6%E6%96%B9%2D%2D%3E-->    <textview android:id="@+id/xuehua_4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_aligntop="@id/xuehua_1" android:layout_toleftof="@id/xuehua_1" android:background="@drawable/iconfont_xuehua">    <!--{cke_protected}{C}%3C!%2D%2D%E5%AE%9A%E4%B9%89%E8%AF%A5%E7%BB%84%E4%BB%B6%E4%BD%8D%E4%BA%8Eview1%E7%9A%84%E5%8F%B3%E6%96%B9%2D%2D%3E-->    <textview android:id="@+id/xuehua_5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_aligntop="@id/xuehua_1" android:layout_torightof="@id/xuehua_1" android:background="@drawable/iconfont_xuehua">    <!--{cke_protected}{C}%3C!%2D%2D%E5%AE%9A%E4%B9%89%E8%AF%A5%E7%BB%84%E4%BB%B6%E4%BD%8D%E4%BA%8E%E7%88%B6%E5%AE%B9%E5%99%A8%E7%9A%84%E4%B8%8A%E6%96%B9%E4%B8%AD%E5%A4%AE%2D%2D%3E-->    <textview android:id="@+id/xuehua_6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:background="@drawable/iconfont_xuehua">    <!--{cke_protected}{C}%3C!%2D%2D%E5%AE%9A%E4%B9%89%E8%AF%A5%E7%BB%84%E4%BB%B6%E4%BD%8D%E4%BA%8E%E7%88%B6%E5%AE%B9%E5%99%A8%E7%9A%84%E5%B7%A6%E8%BE%B9%E4%B8%AD%E5%A4%AE%2D%2D%3E-->    <textview android:id="@+id/xuehua_7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_centervertical="true" android:background="@drawable/iconfont_xuehua">    <!--{cke_protected}{C}%3C!%2D%2D%E5%AE%9A%E4%B9%89%E8%AF%A5%E7%BB%84%E4%BB%B6%E4%BD%8D%E4%BA%8E%E7%88%B6%E5%AE%B9%E5%99%A8%E7%9A%84%E5%8F%B3%E8%BE%B9%E4%B8%AD%E5%A4%AE%2D%2D%3E-->    <textview android:id="@+id/xuehua_8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_centervertical="true" android:background="@drawable/iconfont_xuehua">    <!--{cke_protected}{C}%3C!%2D%2D%E5%AE%9A%E4%B9%89%E8%AF%A5%E7%BB%84%E4%BB%B6%E4%BD%8D%E4%BA%8E%E7%88%B6%E5%AE%B9%E5%99%A8%E7%9A%84%E4%B8%8B%E8%BE%B9%E5%B1%85%E4%B8%AD%2D%2D%3E-->    <textview android:id="@+id/xuehua_9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:background="@drawable/iconfont_xuehua"></textview></textview></textview></textview></textview></textview></textview></textview></textview></relativelayout>
3. Grid layout (GridLayout)  The grid layout is a new layout manager launched after Android4.0. As the name suggests, the grid layout divides the entire container into rows X columns grids, each of which has one component, in addition, you can set how many columns A component spans and how many lines a component spans. The xml attributes and related methods of gridlayout are as follows: 

Xml attributes

Related Methods

Description

Andorid: alignmentMode

SetAlignmentMode (int)

Sets the alignment mode used by the layout manager.

Android: columnCount

SetColumnCount (int)

Set the number of columns in the grid.

Android: columnOrderPreserved

SetColumnOrderPreserved (boolean)

Set whether the empty container of the grid retains column numbers.

Android: rowCount

SetRowCount (int)

Set the number of rows in the grid.

Android: rowOrderPreserved

SetRowOrderPreserved (boolean)

Set whether the Grid container retains row numbers.

Androi: useDefaultMargins

Setdefamarmargins (boolean)

Sets whether the layout manager uses the default margins.

To control the distribution of child components in the GridLayout layout container, GridLayout also provides an internal class: GridLayouut. params, which provides a large number of XML attributes to control the layout distribution of neutron components in the GridLayout layout container. XML Attribute of GridLayout. LayoutParams

Xml attributes

Related Methods

Description

Android: layout_column

 

Set the column number of the Child widget in GridLayout.

Android: layout_columSpan

 

Sets the number of columns that the child widget spans horizontally in GridLayout.

Android: layout_gravity

SetGravity (int)

Sets the method in which the sub-component occupies the space of the grid.

Android: layout_row

 

Set the row number of the Child widget in GridLayout.

Android: layout_rowSpan

 

Sets the number of lines that the child widget spans vertically in GridLayout.

The following uses a calculator as an example to describe how to use GridLayout:
 
          
       
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
  
 
4. frame layout creates a blank area for each component that is added to the frame layout (FramLayout). It is called a frame, and each sub-component occupies one frame, however, these components are automatically aligned according to the grivaty attribute. In fact, it can be understood as a heap of playing cards. One Card is stacked together, and the above card is placed on the following card. Common attributes and related methods of FrameLayout

Xml attributes

Related Methods

Description

Android: foreground

SetForeground (Drawable)

Set the foreground image of the frame layout container

Android: foregroundGravity

SetForegroundGravity (int)

Define the gravity attribute for creating foreground images

 <framelayout android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android">    
     
      
   
  
 </framelayout>
As follows: 5. Absolute layout (AbsoluteLayout)  Absolute layout: You can directly set the component's position by setting the X and Y coordinates of the component. However, the layout manager does not manage the position and size of the Child component, which is completely set by developers. However, it is not recommended to use absolute layout to control the size of sub-components, because the differences between mobile phones running Android on the market differ by, and their screen sizes and resolutions are quite different, with absolute layout, it is impossible to achieve the same effect on various screens. Therefore, the AbsoluteLayout manager is no longer suitable for use in projects. 

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.