Android_layout (i)

Source: Internet
Author: User

Layout---> Android has five major layouts, namely :

    • linearlayout: Linear layout , sub-assemblies are laid out in vertical or horizontal directions.
    • relativelayout: Relative layout , according to the position of the control between the concept of reference arrangement, there is a layout, there is a concept of reference, generally in the relative layout of the control will have the ID of the property.
    • tablelayout: Table layout , inherited from a linear layout. (Similar usage)
    • absolutelayout : absolute Layout , which is positioned by specifying the x/y coordinates of the control (the display is inflexible, is not conducive to development, is almost deprecated).
    • framelayout : frame layout , effect of screen display
one, linear layout (liearlayout)  

Linear layouts are used most in development, with vertical and horizontal orientation
By setting the property "Android:orientation" control direction, the property value is vertical (vertical) and horizontal (horizontal), the default horizontal direction.

Common Properties:

  android:layout_gravity The gravity direction of this element relative to the parent element

  android:gravity The gravity direction of all child elements of this element

  android:orientation Linear layout displays inner child elements in columns or rows

  Android:layout_weight Sub-elements assign weight values horizontally or vertically to unoccupied space


When android:orientation= "vertical", only the horizontal setting will work, and the vertical setting will not work. That is: The left,right,center_horizontal is in force.!!!

When android:orientation= "Horizontal", only the vertical setting will work, and the horizontal setting will not work. That is: The top,bottom,center_vertical is in force.!!!

The difference between android:layout_gravity and android:gravity

  android:gravity works on the element itself-where the element itself is displayed

  android:layout_gravity relative to its parent element-where the element is displayed in the parent element.

such as: Button control

Android:layout_gravity indicates the position of the button on the interface

The android:gravity represents the position of the word on the button on the button.

Optional value [multi-select with ' | ' Separate

Top, bottom, left, right, center_vertical, fill_vertical, Center_horizontal, fill_horizontal, center, fill, Clip_ Vertical

    • Top places the object at the top of its container without changing its size.
    • Bottom places the object at the bottom of its container without changing its size.
    • Left places the object on its container and does not change its size.
    • Right places the object on the left side of its container without changing its size.
    • Center_vertical centers the object vertically without changing its size.

Vertical alignment: Center-aligned vertically.

fill_vertical increase the vertical size of the object to fully fill its container when necessary. Vertical fill

Center_horizontal Center The object horizontally, without changing its size horizontal alignment: Center aligned horizontally

Fill_horizontal increase the horizontal size of the object to fully fill its container when necessary. Fill horizontally

Center centers the object horizontally, without changing its size.

Fill increases the size of the object's longitudinal dimensions as necessary to fully fill its container.

Clip_vertical additional option to cut the contents of the top and/or bottom of the object according to the edges of the container. Clipping is based on its vertical alignment settings: When the top is aligned, the bottom is clipped, the top is clipped at the bottom, and in addition to the top and bottom of the cut. Crop vertically

Clip_horizontal additional option to cut the contents of the left and/or right of the object according to the edges of the container. Clipping is based on its horizontal alignment settings: Cut to the right when aligned to the left, cut to the left when aligned to the right, and cut to the left and right. Crop horizontally

Example

TextView to allow text to be centered vertically/horizontally, there are two things to consider:

1, Layout_width/layout_height is wrap_content, at this time to let TextView on the parent control on the center display, you must set the layout_gravity= "center."

2, Layout_width/layout_height is fill_parent, at this time because TextView already occupy the parent form all the space, must set gravity= "center".

Second, relative layout

The relative layout of the child controls is relative layout based on the reference controls and parameters that they set. The reference control can be either a parent control or another child control, but the referenced control must be defined before the control that references it

Common Properties:

Some of the important attributes that Relativelayout uses:
First Class:property value is True or false
Android:layout_centerhrizontal Horizontal Center
Android:layout_centervertical Vertical Center
Android:layout_centerinparent is completely centered relative to the parent element
Android:layout_alignparentbottom snaps to the bottom edge of the parent element
Android:layout_alignparentleft snaps to the left edge of the parent element
Android:layout_alignparentright snaps to the right edge of the parent element
Android:layout_alignparenttop snaps to the top edge of the parent element
android:layout_alignwithparentifmissing if the corresponding sibling element cannot be found, then the parent element is the reference.
Second Category:The property value must be the reference name of the id "@id/id-name"
Android:layout_below at the bottom of an element
Android:layout_above at the top of an element
Android:layout_toleftof on the left side of an element
Android:layout_torightof on the right side of an element
Android:layout_aligntop aligns the top edge of this element with the top edge of an element
Android:layout_alignleft aligns the left edge of this element with the left edge of an element
Android:layout_alignbottom aligns the bottom edge of this element with the bottom edge of an element
Android:layout_alignright aligns the right edge of this element with the right edge of an element
Third Category:the value of the property is a specific pixel value, such as 30dip,40px
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 top edge of an element
EditText's Android:hint
Sets the prompt information in the input box when EditText is empty.
Android:gravity
The Android:gravity property is the qualification for the view content. For example, a button above the text. You can set the text on the left side of the view, on the right, and so on. Take the button as an example, android:gravity= "right" then the text on the button
Android:layout_gravity
Android:layout_gravity is used to set the position of the view relative to the parent view. For example, a button in the LinearLayout, you want to put the button on the left, right and other positions can be set by this property. Take button For example, android:layout_gravity= "right" button on
Android:layout_alignparentright
Aligns the right end of the current control with the right end of the parent control. This property value can only be true or FALSE, which is false by default.
Android:scaletype:
Android:scaletype is a size that controls how the picture resized/moved to the ImageView. The meaning difference of Imageview.scaletype/android:scaletype value:
Center/center is centered on the original size of the image, and when the picture is longer/wider than the length/width of the view, the center portion of the image is displayed
Center_crop/centercrop proportionally enlarges the size of the image so that the image is long (wide) equal to or greater than the length (width) of the view
Center_inside/centerinside Displays the contents of the picture in full, by scaling it down or the original size to make the picture long/wide equal to or less than the length/width of the view
Fit_center/fitcenter enlarge/Shrink the picture to the width of the view, centered on the display
Fit_end/fitend Zoom in/out to the width of the view, displayed in the lower part of the view
Fit_start/fitstart enlarge/Shrink the image to the width of the view, displayed in the upper part of the view
Fit_xy/fitxy The picture is not scaled up/down to the view size display
The Matrix/matrix is drawn with a matrix and is displayed by zooming in and out of the image.
* * To note that the Drawable folder inside the image name can not be capitalized.

Example:

 1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <?xml version= "1.0" encoding= "Utf-8"?> 3 <relativelayout Xmlns:android= "Http://schemas.android.com/apk/res/android" 4 android:layout_width= "fill_parent" 5 android:layout_     Height= "Fill_parent" 6 > 7 <analogclock 8 android:id= "@+id/aclock" 9 android:layout_width= "Wrap_content" 10 android:layout_height= "Wrap_content" one android:layout_centerinparent= "true"/>12 <digitalclock13 android: Id= "@+id/dclock" android:layout_height= android:layout_width= "wrap_content" Wrap_content "Android:layout" _below= "@id/aclock" android:layout_alignleft= "@id/aclock" android:layout_marginleft= "40px"/>19 <TextVie W20 android:layout_width= "wrap_content" android:layout_height= "wrap_content" \ android:text= "Current time:" droid:layout_toleftof= "@id/dclock" android:layout_aligntop= "@id/aclock"/>25 </relativelayout> 

Three, frame layout (framelayout)

Frame layout, also known as frame layout, is the simplest layout in the five layout, in this layout, the entire interface is treated as a blank spare area, all the child elements can not be specified position, they are placed in the upper left corner of the area, and the following child elements directly over the preceding child elements, Masks the preceding child elements partially and completely. The display is as follows, the first TextView is completely obscured by the second TextView, and the third TextView obscures part of the second TextView

Common Properties:

  Android:foreground: Setting the foreground image of a frame-changing layout container

  android:foregroundgravity: Setting the location of the foreground image display

Example:

  

 1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <framelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "3 android:layout_width=" fill_parent "android:layout_height=" Fill_parent "> 4 <linearlayout android:id= "@+id/linearlayout1" 5 android:layout_height= "match_parent" 6 android:layout_width= "Match_parent" > 7 <Button android:text= "button" 8 android:id= "@+id/button1" 9 android:layout_width= "wrap_content" Ten android:layout_height= " Wrap_content "></button>11 </linearlayout>12 <linearlayout android:layout_width=" Match_parent "13 Android:id= "@+id/linearlayout3" android:layout_height= "match_parent" android:gravity= bottom|right ">16" <button android:text= "button" android:id= "@+id/button3" android:layout_width= "Wrap_content" Android: layout_height= "Wrap_content" ></button>20 </linearlayout>21 <linearlayout android:layout_height= "Match_parent" android:id= "@+id/linearlayout2" android:layout_width= "Match_parent"Android:gravity= "Right" >25 <button android:text= "button" android:id= "@+id/button2" android:layout _width= "Wrap_content" android:layout_height= "wrap_content" ></button>29 </linearlayout>30 < LinearLayout android:layout_width= "match_parent" android:id= "@+id/linearlayout01" android:layout_height=     H_parent "android:gravity=" Bottom|left ">34 <button android:id=" @+id/button01 "android:text=" button "36 Android:layout_width= "Wrap_content" PNs android:layout_height= "wrap_content" ></button>38 </ linearlayout>39 </FrameLayout>

Four, Absolute layout (absolutelayout)

A child control of an absolute layout needs to specify a horizontal ordinate value relative to the layout of this coordinate, otherwise it will be arranged in the upper-left corner as if it were a frame layout. Mobile apps need to adapt to different screen sizes, and this layout model cannot be adapted to the size of the screen, so the application is relatively small.

Common Properties:

  1. Control size

  Android:layout_width: Component Height

  android:layout_height: Component Height

2. Control Position

  android:layout_x: Sets the x-coordinate of the component

  android:layout_y: Sets the y-coordinate of the component

Because of the low application of the layout, different phone, screen size, display layout is not the same, so I have a deep understanding of the

v. Table layout (tablelayout)

  The tabular layout model manages child controls in the form of rows and columns, each of which behaves as a TableRow object, or, of course, a View object. TableRow you can add child controls, one for each column.

Common Properties:

  

android:collapsecolumns: Hide the columns specified in the Tablelayout, and if more than one column needs to be hidden, separate the column numbers that need to be hidden by commas.

android:stretchcolumns: Sets the specified column to be stretched to fill the remaining extra white space , and if more than one column needs to be set to stretch, separate the column numbers that need to be stretched with commas.

android:shrinkcolumns: Sets the columns that are specified to be shrunk. When the Lietaikuan ( too much content ) can be shrunk, the screen is not extruded. When you need to set up multiple columns to shrink, separate the column numbers with commas.

Column element (Button) property: (It is strange that the button does not have Android:layout_column and android:layout_span two attributes, write in no response, do not know why)

Android:layout_colum: Sets the column specified by the control in TableRow.

Android:layout_span: Sets the number of columns that the control spans.

Example:

  

 1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <tablelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "3 android:layout_width=" fill_parent "4 android:layout_height=" fill_parent "5 android:stretchcolumns=" 1 "> 6 <TableRow> 7 <textview 8 android:layout_column= "1" 9 android:padding= "3dip" android:text= "Row1"/>10 < TEXTVIEW11 android:text= "1" android:gravity= "right" android:padding= "3dip"/>14 </tablerow>15 < View16 android:layout_height= "2dip" android:background= "#FF909090"/>18 <tablerow>19 <textview20 android:text= "*" android:padding= "3dip"/>22 <textview23 android:text= "Row12" android:padding= "3dip"/> <textview26 android:text= "2" android:gravity= "right", android:padding= "3dip"/>29 </tablerow>30 <view31 android:layout_height= "2dip" android:background= "#FF909090"/>33 <tablerow>34 <TextView35 android:layout_column= "1" android:text= "Row13" PNs android:padding= "3dip"/>38</tablerow>39 </TableLayout> 

Android_layout (i)

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.