. Net programmers play Android Development --- (7) Relative layout of RelativeLayout and androidlayout

Source: Internet
Author: User

. Net programmers play Android Development --- (7) Relative layout of RelativeLayout and androidlayout
Relative layout RelativeLayout is a common control in Android layout. This control can be used to layout a layout suitable for various screen resolutions. RelativeLayout uses relative positions to set control attributes.

You can set the position between the control and the parent control.

1. Controls and parent container location attributes

Android: layout_alignParentLeft = "true" the child control is left relative to the parent container
Android: layout_alignParentTop = "true" sub-controls depend on the parent container
Android: layout_marginTop = "50dp" sub-control and parent container top margin distance
Android: layout_marginBottom = "50dp" sub-control distance from the bottom margin of the parent container
Android: layout_marginRight = "50dp" sub-control distance from the right of the parent container
Android: layout_marginLeft = "50dp"

Android: layout_centerInParent = "true" // The Child control is centered in the parent container.
Android: layout_centerHorizontal = "true" // The Child control is horizontally centered in the parent container
Android: layout_centerVertical = "true" sub-control vertically center in parent container

The following example shows the layout relative to the parent container.

<? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent"> <TextView android: id = "@ + id/textView1" android: layout_width = "match_parent" android: layout_height = "match_parent" android: text = "relatively easy layout" android: background = "#97 FFFF" android: layout_alignParentLeft = "true" android: layout_alignParentTop = "true" android: layout_marginTop = "50dp" android: layout_marginBottom = "50dp" android: layout_marginRight = "50dp"/> </RelativeLayout>


2. Location attribute between controls

The position attribute between the control and the control. It refers to the position setting between the control and the adjacent control. It mainly has the following attributes:

Android: layout_below = "@ + id/textView1" the control is located below the specified control
Android: layout_toLeftOf = "@ + id/textView1" control is located on the left of the specified control
Android: layout_toRightOf = "@ + id/textView1" control on the right of the specified control
Android: layout_above = "@ + id/textView1" Control located on top of the specified control
Android: layout_alignBaseline = "" The control content is in the same line as the content of the specified control.

Android: layout_alignBottom = "" the bottom of the control is aligned with the bottom of the specified control.

Android: layout_alignLeft = "" this control is aligned with the left side of the specified control

Android: layout_alignRight = "" this control is aligned with the right side of the specified control

Android: layout_alignTop = "" this control is aligned with the top of the specified control

<? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: gravity = "center"> <RelativeLayout android: layout_width = "match_parent" android: layout_height = "110dp" android: id = "@ + id/layone"> <TextView android: id = "@ + id/textView1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: background = "# FFD700" android: text = "tag1"/> <TextView android: id = "@ + id/textView2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_below = "@ + id/textView1" android: background = "# FF0000" android: text = "tag2"/> </RelativeLayout> <RelativeLayout android: layout_width = "match_parent" android: layout_height = "120dp" android: layout_below = "@ + id/layone"> <TextView android: id = "@ + id/textView3" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: background = "# EE9572" android: text = "tag3"/> <TextView android: id = "@ + id/textView4" android: layout_width = "wrap_content" android: layout_height = "60dp" android: layout_toRightOf = "@ + id/textView3" android: background = "# CDAA7D" android: text = "tag 4"/> </RelativeLayout>


3. The following is an example of a product list, which is displayed using RelativeLayout.
<? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent"> <RelativeLayout android: layout_width = "match_parent" android: layout_height = "120dp" android: id = "@ + id/layout1"> <ImageView android: id = "@ + id/imageView1" android: layout_width = "100dp" android: layout_height = "wrap_content" android: layout_alignParentLeft = "true" android: layout_alignParentTop = "true" android: layout_marginLeft = "10dp" android: layout_marginTop = "10dp" android: src = "@ raw/pad"/> <TextView android: id = "@ + id/textView1" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_alignTop = "@ + id/imageView1" android: layout_marginTop = "10dp" android: layout_toRightOf = "@ + id/imageView1" android: text = "Item Name: ipad air "/> <TextView android: id =" @ + id/textView2 "android: layout_width =" fill_parent "android: layout_height =" wrap_content "android: layout_marginTop = "10dp" android: layout_toRightOf = "@ + id/imageView1" android: layout_below = "@ + id/textView1" android: text = "product price: $19 "/> <TextView android: id =" @ + id/textView3 "android: layout_width =" fill_parent "android: layout_height =" wrap_content "android: layout_marginTop = "10dp" android: layout_toRightOf = "@ + id/imageView1" android: layout_below = "@ + id/textView2" android: text = "item color: white "/> </RelativeLayout> <RelativeLayout android: layout_width =" match_parent "android: layout_height =" 3dp "android: background =" # CDAA7D "android: layout_below = "@ + id/layout1" android: id = "@ + id/layout2"> </RelativeLayout> <RelativeLayout android: layout_width = "match_parent" android: layout_height = "120dp" android: layout_below = "@ + id/layout2" android: id = "@ + id/layout3"> <ImageView android: id = "@ + id/imageView2" android: layout_width = "100dp" android: layout_height = "wrap_content" android: layout_alignParentLeft = "true" android: layout_alignParentTop = "true" android: layout_marginLeft = "10dp" android: layout_marginTop = "10dp" android: src = "@ raw/pad"/> <TextView android: id = "@ + id/textView4" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_alignTop = "@ + id/imageView2" android: layout_marginTop = "10dp" android: layout_toRightOf = "@ + id/imageView2" android: text = "Product Name: ipad air"/> <TextView android: id = "@ + id/textView5" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_marginTop = "10dp" android: layout_toRightOf = "@ + id/imageView2" android: layout_below = "@ + id/textView4" android: text = "product price: $19"/> <TextView android: id = "@ + id/textView6" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_marginTop = "10dp" android: layout_toRightOf = "@ + id/imageView2" android: layout_below = "@ + id/textView5" android: text = "color: White"/> </RelativeLayout> <RelativeLayout android: layout_width = "match_parent" android: layout_height = "3dp" android: background = "# CDAA7D" android: layout_below = "@ + id/layout3"> </RelativeLayout>

There are already too many other
Relativelayout layout in Android Development

You can see what layout you are using. If it is LinearLayout, the following controls are arranged with the distance between you. If it is RelativeLayout, You need to specify the method. There are many methods, such as android: layout_below and android: layout_abve, layout_alignParentRight, layout_toRightOf, or layout_toLeftOf

RelativeLayout

<

RelativeLayout xmlns: android = "schemas.android.com/apk/res/android"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

Android: background = "# fd8d8d"

Android: gravity = "center">

<Button

Android: id = "@ + id/button"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: layout_centerVertical = "true"

Android: text = "BUTTON"

Android: textSize = "16sp"/>

<Button

Android: id = "@ + id/button2"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: layout_below = "@ id/button"

Android: layout_toRightOf = "@ id/button"

Android: text = "BUTTON2"

Android: textSize = "16sp"/>

<Button

Android: id = "@ + id/button1"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: layout_above = "@ id/button"

Android: layout_toRightOf = "@ id/button"

Android: text = "BUTTON1"

Android: textSize = "16sp"/>

</

RelativeLayout>... the remaining full text>

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.