Layout file View and ViewGroup, photo album with a summary of the picture

Source: Internet
Author: User
Tags custom name

Control of Android layout files

What the layout file does: Controls the display location of each control you want to display on the screen view and ViewGroup:

Most of the system classes in Android are sub-classes of view, which can be collectively referred to as Android controls that have a special subclass of ViewGroup in view.

When writing layout files, the general rule is: in the layout file root tag Select a viewgroup type or ViewGroup subclass type,

Add another view-type label between the start and end tags of this label.

In conjunction with the structure diagram, the layout file is used in the following rules : Define a root tag for linearlayout or other, add controls such as Button,textview between the start and end tags of the linearlayout based on the page display requirements

There are several commonly used subclasses in ViewGroup, and these commonly used subclasses have a tall name: Android Common layout

Android Common layout:

    1. 1. linearlayout Linear layout
    2. 2. relativelayout relative layout
    3. 3. framelayout frame layout, blank layout
    4. 4. GridLayout Grid Layout
    5. 5. absolutelayout Absolute Layout (not recommended)

LinearLayout Linear Layout

All controls that are placed between the LinearLayout start tag and the label are referred to as child controls of the LinearLayout

All child controls in LinearLayout can only be arranged in a horizontal or vertical line, beyond the screen portion of the display

Common Properties:

1. Control over the control's height: (this property is an essential attribute in the label of the control)

Android:layout_width= ""

Android:layout_height= "" To set the width height of the current control display

How property values are set:

1. Fill_parent or match_parent fills the parent control, which is the same width (high) as the parent control

2. Wrap_content adaptive size, that is, how wide is the content of the control to be displayed, and how much is the final display width of the control?

3. Directly write numbers + units, such as 100DP

2. Control of the direction of alignment:

Android:orientation= "Vertical" specifies the arrangement direction, vertical vertical arrangement, horizontal horizontal arrangement

3. Controls on Control spacing:

To set the properties of the outer spacing of controls:

Android:layout_margin setting the margin between the upper and lower left and right 4 directions at the same time

Android:layout_marginleft= "" Left margin

android:layout_margintop= "" Top margin

android:layout_marginright= "" Right margin

android:layout_marginbottom= "" Bottom margin

Set properties for inner spacing:

Android:padding set the internal margin in 4 directions at the same time

Android:paddingleft left inner margin

Android:paddingtop on-side inner margin

Android:paddingright right inner margin

Android:paddingbottom bottom inner margin

The difference between the padding and the margin is that the margin is set to the spacing between the control and the parent control, or the spacing of other sibling controls, such as setting the upper space between the red area and the overall screen

The padding is set by the spacing of the content within the control from the edge of the control's range

Set the control's special display position (right, center, and so on):

Android:layout_gravity

Note: This property has a system default problem when using this property:

When in a vertical linear layout, any properties related to height (vertical) are invalidated, such as: bottom,center_vertical, etc.

When in a horizontal linear layout, any properties related to width (horizontal) are invalidated, such as: Right,center_horizontal, etc.

Reason: With vertical linear layout as an example, the official think that in this layout can be placed from top to bottom of the infinite child control, although the part of the screen cannot be displayed, but still exist, the program will not error

The difference between android:gravity and android:layout_gravity:

android:layout_gravity controls where the current control is located within the scope of the parent control

Android:gravity controls the position of the contents of the current control within the control's wide height range

Controls on control weights:

Android:layout_weight weight Sets the content of the current control to occupy a fraction of the remaining space

Android:weightsum weight sum (can be omitted without setting)

Relativelayout Relative Layout

The default position for all controls is the upper-left corner, and if you want to customize the position of the control, the location needs to be done relative to the other controls.

Such as: There is control a,b,c a position in the upper left corner, B in the lower right corner of the screen, C is located in the lower right of a

Common properties to use for relative layout child controls:

1. centering the parent control as a reference, close to an edge of the parent control, or within the scope of the parent control

Property value Fill in: True or FALSE, write false if the effect is the same as not writing this property

android:layout_alignparentleft="" clings to the left edge of the parent control

android:layout_alignparenttop="" to the top edge of the parent control

android:layout_alignparentright="" to the right edge of the parent control

android:layout_alignparentbottom="" clings to the bottom edge of the parent control

Android:layout_centerhorizontal="" horizontally centered within the scope of the parent control

Android:layout_centervertical="" centers vertically within the scope of the parent control

android:layout_centerinparent="" is fully centered within the scope of the parent control

How to set a unique identity for a control: adding to a control Android : ID property, adding a property value to the @+id/ Custom Name

Note: When you are in the same layout file, the control and the control ID cannot be duplicated, but if you are in a different layout file, ID can repeat

2. relative to the sibling control, located in the upper and lower positions of the sibling control

Fill in the value of the property: the sibling control that is filled in as a reference is uniquely identified

android:layout_above="@+id/dhj" sets the current control at the top of the control's upper control , with the sibling control's flush position, such as the left cloud piece complete, such as: there is space

android:layout_below="@+id/dhj" sets the current control at the top of the control under the control's sibling control, with the horizontal position of the brother control, such as the left cloud piece complete, such as: there is space

android:layout_toleftof="@+id/dhj" sets the current control at the top of the control's left control, with the sibling control's flush position, such as the left cloud piece complete, such as: there is space

android:layout_torightof="@+id/dhj" sets the current control to the right of the top control, the control's sibling control, and the horizontal position of the brother control, such as the left cloud, such as: there is space

3. Relative to the sibling control, with the sibling control of the horizontal position, such as the left edge leveling and other effects

Fill in the value of the property: the sibling control that is filled in as a reference is uniquely identified

android:layout_alignleft="" sets the current control to be flush with the left edge of the specified control

android:layout_aligntop="" sets the current control to be flush with the top edge of the specified control

android:layout_alignright="" sets the current control to be flush with the right edge of the specified control

android:layout_alignbottom="" to set the current control to the bottom edge of the specified control

Android:layout_alignbaseline="" sets the current control's alignment with the specified control baseline

Framelayout Frame Layout

Typically, it is used in conjunction with fragment, with few individual uses

Characteristics:

1. Default upper left corner of all control locations

2. Similar to Photoshop, each control occupies one layer, that is, if you add multiple controls at the same time, the controls overlap together.

If you need control control location: android:layout_gravity

GridLayout Grid Layout

Features: More for the layout of the grid structure in the calculator or dial-up page

Common Properties:

Attribute values are added directly to the function: Set the total number of rows in the grid and the number of columns

Android:rowcount= "" Line number

Android:columncount= "" Number of columns

Specifies that the current control is in the first row of the grid

Row and column indexes start at 0

android:layout_row= "0"

android:layout_column= "0"

Note: Do not cross the line, such as: There are 4 columns in total, Android:layout_column property value specified 5

Stretches the width of the current control to a width of 2 columns

Android:layout_columnspan= "2"

android:layout_gravity= "Fill"

To stretch the current control height to a height of 2 rows

Android:layout_rowspan= "2"

android:layout_gravity= "Fill"

Absolutelayout Absolute Layout

Specifies the x, Y coordinate value on the top left of the current control, which is based on the entire phone

android:layout_x= "10DP"

Android:layout_y= "10DP"

Because it is not suitable for the screen, discarded

Summary: So much layout?? When do you use that kind of layout??

    1. When you actually write a project, you rarely use absolutelayout
    2. If the layout effect you want to implement has a grid effect, we recommend that you use GridLayout
    3. In most cases, the relative layout is recommended, if the relative layout is not resolved (implementing the width 1:2:1 between the n controls) then use a linear layout
    4. Or, when the structure of the page is simple, just horizontal or vertical arrangement can also be directly used linear layout
    5. Framelayout General with fragment use

Photo Album has a summary of the picture

Layout file View and ViewGroup, photo album with a summary of the picture

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.