Layout_gravity, gravity, orientation, and layout_weight In the android Layout

Source: Internet
Author: User

In linear layout, there are four important parameters that directly determine the layout and position of elements. These four parameters are
Android: layout_gravity (the gravity direction of the element relative to the parent element)
Android: gravity (the gravity direction of all child elements of the current element)
Android: orientation (linear layout displays internal child elements in columns or rows)
Android: layout_weight (assign the weight value to the child element in the linear layout for the space not occupied [horizontal or vertical]. The smaller the value, the larger the weight.
The premise is that the sub-element sets the android: layout_width = "fill_parent" attribute (horizontal direction)
Or android: layout_height = "fill_parent" Property (vertical direction)


If the android: layout_width = "wrap_content" of a child element"
Or android: layout_height = "wrap_content ",
Then, the value of android: layout_weight is the opposite for space allocation in this direction.

The following is a simple example to describe these four parameters:

 < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"                           android:layout_height = "200dp"                           android:layout_width = "200dp"                           android:background = "#AABBCC"                           android:orientation= "horizontal"                           android:layout_gravity= "center" >                           < TextView android:text = "ONE"                                               android:background = "#aa0000"                                               android:layout_height = "wrap_content"                                               android:layout_width = "wrap_content"                                               android:layout_margin = "1dp" />                            < TextView android:text = "TWO"                                               android:background = "#aa0000"                                               android:layout_height = "wrap_content"                                               android:layout_width = "wrap_content"                                               android:layout_margin = "1dp" />
 
Note: In the preceding example, the root layout is LinearLayout, which contains two TextView views. In order to have an intuitive understanding of the parameter android: layout_gravity, three parameters are specially added to the root layout LinearLayout.
Android: layout_height = "200dp"
Android: layout_width = "200dp"
Android: background = "# AABBCC"
Specify a fixed width, height, and background color for the layout. The effect of the preceding example is as follows:


NOTE: For the parameter android: layout_gravity in LinearLayout, it means to specify the gravity direction of the layout relative to the parent layout. Because the layout is already the root layout, its parent layout is the entire screen, if this parameter is set relative to the screen position, you can test it with parameters such as top "bottom | left | right.
Now add the following complete XML parameter android: gravity = "bottom | right" to check the effect:

 < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"                      android:layout_height = "200dp"                      android:layout_width = "200dp"                      android:background = "#AABBCC"                      android:orientation="horizontal"                      android:layout_gravity= "center"                      android:gravity = "bottom|right " >                      < TextView android:text = "ONE"                                     android:background = "#aa0000"                                     android:layout_height = "wrap_content"                                     android:layout_width = "wrap_content"                                     android:layout_margin = "1dp" />                      < TextView android:text = "TWO"                                     android:background = "#aa0000"                                     android:layout_height = "wrap_content"                                     android:layout_width = "wrap_content"                                     android:layout_margin = "1dp" />
 

Vcq1vMrQp7n7oaM8YnI + Cjxicj4KPGJyPgqyzsr9IGFuZHJvaWQ6b3JpZW50YXRpb249IA = "horizontal" determines that each sub-element occupies one column.
The android: orientation = "vertical" parameter indicates that each sub-element occupies one row, that is, it is arranged from top to bottom.


For the child element of the LinearLayout layout, add the android: layout_weight parameter to each child element.

 < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"                      android:layout_height = "200dp"                      android:layout_width = "200dp"                      android:background = "#AABBCC"                      android:layout_gravity = "center"                      android:gravity = "bottom|right"                      android:orientation = "horizontal" >                      < TextView android:text = "ONE"                                     android:background = "#aa0000"                                     android:layout_height = "wrap_content"                                     android:layout_width = "wrap_content"                                     android:layout_margin = "1dp"                                     android:layout_weight = "1" />                      < TextView android:text = "TWO"                                     android:background = "#aa0000"                                     android:layout_height = "wrap_content"                                     android:layout_width = "wrap_content"                                     android:layout_margin = "1dp"                                     android:layout_weight = "2" />
 


Text is the weight of ONE, but the obvious proportion of width is smaller than that of TWO. I was puzzled. Later I learned that if the TextView parameter android: layout_width = "wrap_content" change all to android: layout_width = "fill_parent", then OK, the Code is as follows:

 < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"                      android:layout_height = "200dp"                      android:layout_width = "200dp"                      android:background = "#AABBCC"                      android:layout_gravity = "center"                      android:gravity = "bottom|right"                      android:orientation = "horizontal" >                      < TextView android:text = "ONE"                                     android:background = "#aa0000"                                     android:layout_height = "wrap_content"                                     android:layout_width = " fill_parent "                                     android:layout_margin = "1dp"                                     android:layout_weight = "1" />                      < TextView android:text = "TWO"                                     android:background = "#aa0000"                                     android:layout_height = "wrap_content"                                     android:layout_width = " fill_parent "                                     android:layout_margin = "1dp"                                     android:layout_weight = "2" />
 


Related Article

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.