Android Development Key Difficulty: Relativelayout (relative layout) detailed

Source: Internet
Author: User

Https://i.cnblogs.com/EditPosts.aspx?opt=1 Key Knowledge

As with the linear layout (linearlayout), the Relaivelayout relative layout is one of the more layouts we use. Relative, as the name implies is a reference, is determined by a sibling component, or the parent container (the sibling component is a component in the same layout, if one component in the layout refers to another component in the layout error). The adaptive problem of different screen resolution can be solved by reasonably using the weight weight attribute and relativelayout relative layout of linearlayout.

For example, Xiao Ming on the way to school, at this time his position can be left home how many meters or from the school how many meters, is the use of different reference.

Well, don't say a lot of nonsense, just say more commonly used properties bar ~

Set how all the components in the layout are aligned:

Android:gravity: Sets the alignment of the sub-components within the container

Android:ignoregravity: If this property is set for which component, then the component is not affected by the gravity property

To locate according to the parent container:

Which property is set to TRUE where you want to be located

Left-justified: Android:layout_alighparentleft

Right-aligned: Android:layout_alighparentright

Top alignment: Android:layout_alighparenttop

Bottom alignment: Android:layout_alighparentbottom

Horizontal Center: Android:layout_centerhorizontal

Center vertically: android:layout_centervertical

Central location: android:layout_centerinparent

Last Picture ~ (A little ugly ...) People look at it ~)

Positioning according to sibling components (the property value on the right is the ID of the sibling component)

Left: Android:layout_toleftof

Right: Android:layout_torightof

Top: Android:layout_above

Below: Android:layout_below

Align Upper boundary: android:layout_aligntop

Align bottom boundary: android:layout_alignbottom

Align left boundary: Android:layout_alignleft

Align right boundary: Android:layout_alignright

Here is a more typical example ~

Plum Blossom Layout:

The relevant code is as follows:

<relativelayout xmlns: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 center of the container--

<imageview
Android:id= "@+id/img1"
Android:layout_width= "80DP"
android:layout_height= "80DP"
Android:layout_centerinparent= "true"
android:src= "@drawable/pic1"
/>

<!--to the left of the middle picture--
<imageview
Android: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--
<imageview
Android: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--
<imageview
Android:id= "@+id/img4"
Android:layout_width= "80DP"
android:layout_height= "80DP"
Android:layout_above= "@id/img1"
Android:layout_centerhorizontal= "true"
android:src= "@drawable/pic4"
/>

<!--in the middle of the picture--
<imageview
Android: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>

Finally, there are two more commonly used margin and padding properties!

Margin: Sets the margin of the component to the parent container (usually the layout)

Android:layout_margin: Specifies that a certain margin is left outside the perimeter of the control
Android:layout_marginleft: Specifies that the left side of the control leaves a certain margin outside
Android:layout_margintop: Specifies that a certain margin is left outside the top of the control
Android:layout_marginright: Specifies that a certain margin is left outside the right side of the control
Android:layout_marginbottom: Specifies that a certain margin is left outside the bottom of the control

Padding: Sets the padding between elements within the component (which can be understood as padding)

Android:padding: Specifies that a certain margin is left within the perimeter of the specified control
Android:paddingleft: Specifies a margin within the left side of the specified control
Android:paddingtop: Specifies that a certain margin is left inside the top of the control
Android:paddingright: Specifies a margin within the right side of the specified control
Android:paddingbottom: Specifies that a certain margin is left inside the bottom of the specified control

Both are followed by a parameter, usually in DP units, Eg:android:margin = "10DP"

As follows:

Related code:

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
Tools:context= ". Mainactivity ">

<button
Android:id= "@+id/btn1"
android:layout_height= "Wrap_content"
Android:layout_width= "Wrap_content"
android:text= "button"
/>
<button
android:paddingleft= "100DP"
android:layout_height= "Wrap_content"
Android:layout_width= "Wrap_content"
android:text= "button"
android:layout_torightof= "@id/btn1"
/>

Code Explanation:

This code is simple enough to write a combination of two buttons.

The second button of the first combination is set to Paddingleft = "100DP:, the result button is stretched by 100DP because the element spacing inside is filled with 100DP;

The second button of the second combination is set to MarginLeft = "100DP", and the result button shifts 100DP to the right.

Summarize

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 class: The attribute 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

Class III: Attribute values are specific pixel values, 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 by the original size of the picture is displayed, when the picture length/width exceeds the length/width of the view, then the center portion of the image is displayed;

Center_crop/centercrop proportionally enlarge the size of the image, so that the image length (width) is equal to or greater than the length (width) of the view;

Center_inside/centerinside the contents of the picture to the full center display, by proportionally reduced or the original size so that the picture length/width is equal to or less than the length/width of the view;

Fit_center/fitcenter enlarge/Reduce the picture to the width of the view, center display;

Fit_end/fitend enlarges/shrinks the image to the width of the view, which is displayed in the lower part of the view;

Fit_start/fitstart enlarges/shrinks the image to the width of the view, which is 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.

Note that the name of the image in the Drawable folder cannot be capitalized.

Android Development Key Difficulty: Relativelayout (relative layout) detailed

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.