Summary of linear and relative la s

Source: Internet
Author: User

Android layout attributes reference:
Http://blog.csdn.net/webrobot/article/details/7425078

Notes on Linear layout are as follows:

Android: layout_margintop attributes
It refers to the space above the view or layout (such as linear layout.
Do not mistakenly consider the distance from the top of the parent element (unless it is the first control of the parent element ).
However, this is not the case in the relative layout. In the relative layout, it refers to the distance from the top of its parent element !!! (For this error, see below)
Error analysis:
In linear layout, because it is always vertical or horizontal, it is ordered, so this margin is relative to the parent control.
However, in the relative layout, the control is superimposed from one position. Therefore, you must specify the distance from a control to the top.
However, if the control is specified under the B control through Android: layout_below, The layout_margintop
It refers to the distance between A and B, rather than the distance from the top of the parent control. The key is to have a deep understanding of the relative layout.
"Relative"

Android: layout_gravity attributes
How to place the child view relative to the parent container

Android: gravity attributes
Specify the gravity direction of this element or all its child elements (that is, how to layout )!!
For example, if the text in a button is specified at the bottom, Android: gravity = "bottom" can be used"
For example, Android: gravity = "Left" can be used to specify that all child elements in a layout depend on the left"

The gravity attribute description of linerlayout:
Http://www.cnblogs.com/olvo/archive/2012/05/04/2482440.html
You can find this in the layout file.

Note:
1 Android: layout_gravity and Android: layout_weight
The subview can be applied only when it is in linearlayout or tablelayout !!!
2 Android: gravity can be used in both linear and relative la s !!!!

Examples and Analysis of Android: layout_gravity ineffective in linearlayout:
References:
Http://www.cnblogs.com/olvo/archive/2012/05/21/2511632.html
Note:
Because the default linearlayout is: Android: Orientation = "horizontal"
Conclusion:
Android: layout_gravity = "? "
This parameter takes effect only when it is set to horizontal. For example, left, right, and center_horizontal.

Android: layout_gravity = "? "
It takes effect only when it is set to vertical. For example, top, bottom, and center_vertical.

You can also do this: Set Android: gravity = "right" Android: Orientation = "horizontal" in linearlayout"
Then, you can set Android: layout_gravity = "right" in the sub-view)

Cause analysis:
Because Android: Orientation specifies the overall layout direction, from left to right or from top to bottom.
For example, Android: Orientation = "vertical" should follow the top-down mode in general. You cannot change the vertical layout of the subview at will.
You can change its position horizontally within a certain distance. When Android: Orientation = "horizontal", the principle is similar.

Therefore, in linear layout, Android: layout_gravity = "bottom | right" cannot be placed in the lower right corner.
You can use the relative layout to achieve this:
Android: layout_alignparentright = "true"
Android: layout_alignparentbottom = "true"
In this way, the bottom right corner is reached.

Let's continue to look at a small problem here:

<?xml version="1.0" encoding="utf-8"?><LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="right"    android:orientation="horizontal" >    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginRight="20dip"        android:layout_gravity="right"        android:text="Button1" />    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginRight="10dip"        android:text="Button2" />   </LinearLayout>

 

Why is button1 not on the rightmost side, but button2 on the rightmost side?
1 is horizontal, that is, arranged from left to right.
2 Android: gravity = "right" means that all controls depend on the right.
So first, button1 is on the right, but then button2 is on the right.

 

 

Notes on relative layout are as follows:
Important references:
Http://blog.csdn.net/johnny901114/article/details/7865617 this information is good
The Comparison Between http://blog.csdn.net/huangxy10/article/details/8181082 and linear layout is quite good.
However, this information is incorrect:
Android: layout_gravity and android: layout_weight
The subview can be applied only when it is in LinearLayout or TableLayout !!!!!
This comparison provides a conclusion: whether a property can play a role in the control depends on its parent layout.

Example 1:
There is a TextView control in a RelativeLayout, so it has the android: layout_centerInParent = "" attribute
However, the TextView control in LinerLayout does not have the android: layout_centerInParent = "" attribute !!!!!!!

Example 2:
Linear la s do not have the following Alignment Methods:
Android: layout_centerHorizontal is true, and the control is placed horizontally in the center;
Android: layout_centerVertical is true, and the control is placed vertically in the center;
Android: layout_centerInParent is true. Place the control in the center of the parent control (horizontal and vertical center are set at the same time );
However, when the parent element of a linear layout is relative, it also has these attributes !!!!

Summary:
1. The relative layout does not have the property Android: orientation, but if it is written, no error will be reported.
2. In the relativelayout layout, the center is implemented using Android: layout_centerxxx.
Android: gravity is used to center the linerlayout layout.

There is such a situation
In the relative layout, the control layout is as follows:

 <ImageButton            android:id="@+id/report_left"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentLeft="true"            android:layout_centerVertical="true"            android:layout_marginLeft="5dp"            android:background="@drawable/tanteimodoru"            android:padding="5dp" />

Note:
Android: layout_alignparentleft = "true" and Android: layout_marginleft = "5dp"
That is to say, layout_marginleft is set relative to the left alignment of the parent control.
Therefore, imagebutton does not really align to the left, and there is a 5dp distance.

 

 

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.