Before using layout, we need to understand two attributes:
Vertical layout: Android: layout_height= "Wrap_content"
Horizontal layout: Android: layout_width= "Match_parent"
Wrap_content indicates that the package content is not filled. match_parent indicates that the parent container is full. fill_parent and match _ parent mean the same. They can be used in more than 2.2 words, and fill_parent is used in less than 2.2 words.
Use framelayout Layout
The framelayout object is like setting a blank area in advance on a screen, and then filling in the element. All elements are placed at the leftmost of the region and cannot be specified for these elements
<Framelayout Android: Id = "@ + ID/framelayout1"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content">
<Button Android: layout_height = "wrap_content" Android: text = "@ string/OK" Android: Id = "@ + ID/button1" Android: layout_width = "match_parent"> </button>
<Button Android: text = "button" Android: Id = "@ + ID/button2" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </button>
</Framelayout>
When two buttons are placed, the two buttons are combined.
Use linearlayout Layout
A commonly used Layout Method in Android. It arranges its child elements in one direction (horizontal or vertical ).
<Linearlayout
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: Orientation = "vertical">
</Linearlayout>
After adding two buttons to the code above, the interface is as follows:
You can useAndroid: Orientation = "vertical"OrAndroid: Orientation = "horizontal"To set horizontal or vertical arrangement
Use relativelayout Layout
This indicates the relative layout, and the elements in it are calculated according to the relative position. You can specify the position of an internal element a to Element B.
Anroid also has the concept of padding and margin. Padding indicates padding, while margin indicates padding.
Layout Design: alignment with parent elements, alignment with relative elements, and spacing.
1. Align attributes with parent elements:
Android: layout_centerhrizontal horizontal center
Android: layout_centervertical vertical center
Android: layout_centerinparent is completely centered over the parent Element
Android: layout_alignparentbottom: attach the lower edge of the parent Element
Android: layout_alignparentleft: Stick the left edge of the parent Element
Android: layout_alignparentright: attaches the right edge of the parent element.
Android: layout_alignparenttop: top the parent Element
Android: layout_alignwithparentifmissing
2. Align attributes with relative elements:
Android: layout_below is under an element
Android: layout_abve is above an element.
Android: layout_toleftof on the right of an element
Android: layout_aligntop: Align the top edge of an element with the top edge of an element.
Android: layout_alignleft: Align the left edge of an element with the left edge of an element.
Android: layout_alignbottom: the bottom edge of the current element is aligned with the bottom edge of an element.
Android: layout_alignright: the right edge of an element is aligned with the right edge of an element.
3. Spacing property:
Android: layout_marginbottom distance from the bottom edge of an element
Android: layout_marginleft distance from the left edge of an element
Android: layout_marginright distance from the right edge of an element
Android: layout_margintop distance from the edge of an element
Measurement Units in Android include:
Px pixel, dip depends on the pixel of the device, SP pixel with a ratio, PT point, in inch, Mm mm
Use tablelayout Layout
Is a table layout that is arranged in rows and columns. It contains tablerow that defines each row. You can add the required elements to each row.
<TablelayoutAndroid: Id = "@ + ID/tablelayout1" Android: layout_width = "match_parent" Android: layout_height = "wrap_content">
<Tablerow Android: Id = "@ + ID/tablerow1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content">
<Textview Android: Id = "@ + ID/textview1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "textview"> </textview>
<Edittext Android: text = "edittext" Android: Id = "@ + ID/edittext1" Android: layout_width = "wrap_content" Android: layout_height = "30px"> </edittext>
</Tablerow>
<Tablerow Android: Id = "@ + ID/tablerow2" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content">
<Textview Android: Id = "@ + ID/textview1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "textview"> </textview>
<Edittext Android: text = "edittext" Android: Id = "@ + ID/edittext1" Android: layout_width = "wrap_content" Android: layout_height = "30px"> </edittext>
</Tablerow>
</Tablelayout>