Android layout manager and android layout manager
Content
- Environment
- Project Structure
- Linear Layout
- Table Layout
- Frame Layout
- Relative Layout
- Grid layout
- Absolute Layout
I recently wrote my own Andorid APP and encountered the problem of drawing the interface. It was a headache to start the background and see the painting interface, but it seems that I can't do it without studying it ~ At least the Andorid interface is much simpler than the Web interface. Just try the attributes on your own ~
Download Demo Environment
- Windows 2008 R2 64-bit
- Eclipse ADT V22.6.2, Android 4.4.2 (API 19)
- SAMSUNG GT-8618, 3.5 inch screens (7.5x5 cm, same apple size), Android OS 4.1.2
Project Structure
Figure 1.1 project structure diagram 1.2 Main Program
Linear Layout
The LinearLayout layout will arrange the components in the container one by one. LinearLayout can control the horizontal arrangement of each component (throughAndroid: orentationAttribute control), you can also control the vertical arrangement of each component.
The LinearLayout layout does not contain line breaks. When the components are arranged in the header one by one, the remaining components are not displayed.
Figure 2 linear Layout
Note: There are 3 textviews and 15 buttons on the page. The outermost layer isAndroid: orientation = "vertical"Vertical layout. In the vertical layout, the buttons are from 1 ~ 10 adoptAndroid: orientation = "horizontal"Horizontal layout, but only the button 1 ~ 5. The buttons are displayed ~ 15 vertical layout is also adopted andAndroid: gravity = "bottom | center_horizontal"Attribute, that is, the bottom and horizontal center.
Table 1 common XML attributes of LinearLayout
XML attributes |
Description |
Android: baselineAligned |
Set this attributeFalseWill prevent the layout manager from alignment with its child elements |
Android: divider |
Split between two buttons when setting Layout |
Android: gravity |
Sets the alignment of components in the layout manager. For exampleTop,Left,Center_verticalYou can also specify a combination of multiple alignment methods, suchLeft | center_vertical, Indicating the center on the left and vertical of the screen |
Android: measureWithLargestChild |
When the attribute isTrueAll child elements with weight have the minimum size of the Maximum child element. |
Android: orientation |
Set the arrangement of components in the layout managerHorizontal,VerticalOne |
Android: layout_gravity |
Specifies the alignment of the child element in LinearLayout. |
Android: layout_weight |
Specify the weight of the child element in LinearLayout. |
The method corresponding to the attribute isSet ***It is easy to recognize. The same below.
Table Layout
The TableLayout layout manages components in the form of rows and columns. Instead of explicitly declaring the number of rows and columns, it adds TableRow and other components to control the number of rows and columns of the table.
TableLayout inherits LinearLayout, so it is essentially a linear layout manager.
Figure 3-1 table layoutFigure 3-2 table layout
Table 2 common XML attributes of TableLayout
XML attributes |
Description |
Android: collapseColumns |
Set the sequence number of the columns to be hidden. Multiple column numbers are separated by commas (,). |
Android: shrinkColumns |
Set the number of columns allowed to be shrunk. Multiple column numbers are separated by commas (,). |
Android: stretchColumns |
Set the sequence numbers of columns that can be stretched. Multiple column numbers are separated by commas (,). |
Frame Layout
The FrameLayout layout creates a blank area (called a frame) for each component that is added to it. Each child component occupies one frame, which is automatically aligned according to the gravity attribute. The effect of the frame layout is similar to that of the CardLayout of AWT, which overlays components one by one.
Figure 4 frame Layout-neon lights
Table 3 common XML attributes of FrameLayout
XML attributes |
Description |
Android: foreground |
Set the foreground image of the frame layout container |
Android: foregroundGravity |
Define the gravity attribute for creating foreground images |
Relative Layout
The position of the Child component in the RelativeLayout layout is always determined by the sibling component and parent container. Therefore, it is called relative layout.
If the position of component A is determined by the position of component B, Android requires that component B be defined before component A be defined.
Figure 5-1 relative LayoutFigure 5-2 relative Layout-plum blossom
Table 4.1 RelativeLayout XML attributes
XML attributes |
Description |
Android: gravity |
Sets the alignment of each sub-component in the layout container. |
Android: ignoreGravity |
Set which component is not affected by the gravity attribute |
To control the layout of child components in the layout container, RelativeLayout provides an internal class: RelativeLayout. LayoutParams, which provides a large number of XML attributes to control the layout of child components in RelativeLayout.
Table 4.2 RelativeLayout. LayoutParams can only be set as a boolean attribute.
XML attributes |
Description |
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 sub-component is aligned with the bottom of the layout container. |
Android: layout_alignParentLeft |
Determines whether the child widget is aligned with the left side of the layout container. |
Android: layout_alignParentRight |
Determines whether the child widget is aligned with the right side of the layout container. |
Android: layout_alignParentTop |
Determines whether the child widget is aligned with the top of the layout container. |
Table 4.3 RelativeLayout. LayoutParams can only be set to attributes of other UI component IDs.
XML attributes |
Description |
Android: layout_toRightOf |
Control the child widget to the right of the given ID widget |
Android: layout_toLeftOf |
Controls that the child widget is located on the left of the given ID widget. |
Android: layout_abve |
Control the sub-component to be located above the specified ID component |
Android: layout_below |
Control the sub-component to be located below the specified ID component |
Android: layout_alignTop |
Controls the upper boundary alignment of a child widget with a given ID |
Android: layout_alignBottom |
Controls the sub-component to be located at the bottom boundary alignment of the given ID component. |
Android: layout_alignLeft |
Controls the left boundary alignment of a child widget with a given ID |
Android: layout_alignRight |
Controls the right boundary alignment of a child widget with a given ID |
Grid layout
GridLayout layout is a new layout manager for Android 4.0. Before that, if you want to achieve the grid effect, the most common choice is to use the LinearLayout layout. Secondly, you can use the TabelLayout layout, but there are all problems. What problems have been solved now, I will not talk about it any more.
The role of GridLayout is similar to the table label of HTML. It divides the entire container into rows x column grids, and each grid can be placed with one component. You can also set how many columns A component spans and how many rows a component spans.
Figure 6 grid layout-calculator
Table 5 GridLayout XML attributes
XML attributes |
Description |
Android: alignmentMode |
Sets the alignment mode used by the layout manager. |
Android: columnCount |
Set the number of columns in the grid. |
Android: columnOrderPreserved |
Set whether the Grid container retains column numbers. |
Android: rowCount |
Set the number of rows in the grid. |
Android: rowOrderPreserved |
Set whether the Grid container retains row numbers. |
Android: usedefamarmargins |
Sets whether the layout manager uses the default margins. |
The absolute layout AbsoluteLayout layout does not provide any layout control, but allows developers to control the component position through the X and Y coordinates themselves. When the AbsoluteLayout layout is used, the layout container no longer manages the positions and sizes of child components-all of which must be controlled by developers themselves.
Figure 7-1 absolute LayoutFigure 7-2 absolute Layout
Note: The AbsoluteLayout layout is outdated. Mobile phones running Android applications are often very different, so there may be large differences in screen size and differentiation rate. It is difficult to take into account different screen sizes and resolutions when using absolute layout.
Download Demo
What layout manager is available for android?
LinearLayout, TableLayout, RelativeLayout, and FrameLayout also have an unrecommended AbsoluteLayout
LinearLayout is commonly used for beginners. Later, RelativeLayout ~~ will be used for writing interfaces ~~
How to change the default layout of the layout manager in eclipse
Right-click layout, click new, and select android XML file. The default value is Linearlayout.