The weight property of linear layout is convenient in equal scale distribution, but for complex interface, nesting multi-layer linearlayout layout will cause rendering to be slower, occupy more system resources, and using relativelayout, it may just need one layer to complete, You can set the display position of the component in the parent container or sibling component reference +margin +padding.
1. Container positioning
The parent container positioning property shows:
Brother Container Positioning Properties:
Example: Plum blossom layout
The implementation code is as follows:
<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:id= "@+id/relativelayout1"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <!--This is in the middle of the container. - <ImageViewAndroid:id= "@+id/img1"Android:layout_width= "80DP"Android:layout_height= "80DP"android:layout_centerinparent= "true"android:src= "@drawable/pic1"/> <!--on the left of the middle picture - <ImageViewAndroid:id= "@+id/img2"Android:layout_width= "80DP"Android:layout_height= "80DP"Android:layout_toleftof= "@id/img1"android:layout_centervertical= "true"android:src= "@drawable/pic2"/> <!--to the right of the middle picture - <ImageViewAndroid:id= "@+id/img3"Android:layout_width= "80DP"Android:layout_height= "80DP"Android:layout_torightof= "@id/img1"android:layout_centervertical= "true"android:src= "@drawable/pic3"/> <!--on top of the middle picture - <ImageViewAndroid:id= "@+id/img4"Android:layout_width= "80DP"Android:layout_height= "80DP"Android:layout_above= "@id/img1"Android:layout_centerhorizontal= "true"android:src= "@drawable/pic4"/> <!--at the bottom of the middle picture - <ImageViewAndroid:id= "@+id/img5"Android:layout_width= "80DP"Android:layout_height= "80DP"Android:layout_below= "@id/img1"Android:layout_centerhorizontal= "true"android:src= "@drawable/pic5"/> </Relativelayout>
2. The difference between margin and padding
Margin represents the component's distance from the edge of the container, such as: MarginLeft = "5DP" for component distance to the left edge of container 5DP
Padding stands for padding, for example: TextView setting paddingleft = "5DP", which fills 5dp of space on the left side of the element in the component.
Margin is for the components in the container , and padding is for the elements in the component .
Android Layout Relativelayout