Android Layout-relative Layout)

Source: Internet
Author: User

This article translated from: http://developer.android.com/guide/topics/ui/layout/relative.html

Relativelayout is a view group that uses relative positions to display sub-views. The position of each view can be specified relative to adjacent elements (such as the left or bottom side of the other view), or relative to the parent relativelayout region (such as bottom alignment, center to left ).

Relativelayout is a very powerful tool used to design user interfaces, because it can eliminate nested view groups and flatten your layout layers, which can improve performance. If you find that you have used several nested linearlayout groups, you can use a single relativelayout instead.

View Positioning

Relativelayout allows the child view to specify their positions relative to the parent view or adjacent view (specified by ID. Therefore, you can align the two elements along the left border, or align them up and down in the center, and so on. By default, all child views are drawn in the upper left corner of the layout. Therefore, you must use various layout attributes from relativelayout. layoutparams to define the position of each view.

The following are valid layout attributes in relativelayout:

Android: layout_alignparenttop

If the attribute value is "true", the top edge of the view must match the top edge of its parent view.

Android: layout_centervertical

If the attribute value is "true", it is vertically centered within the parent view of the view.

Android: layout_below

The upper edge of the view is located under the view specified by the resource ID.

Android: layout_torightof

The left edge of the view is located on the right of the view specified by the resource ID.

These are just some examples. All layout attributes are in the relativelayout. layoutparams document.

 

Each layout property value may be a Boolean value relative to its parent relativelayout, or it may be the ID of the adjacent view in the layout.

In your xml layout, you can declare the dependency on another view in the layout in any order. For example, you can declare that view1 is at the bottom of view2, even if view2 is finally declared in the layout hierarchy. The following example demonstrates this scenario.

<? XML version = "1.0" encoding = "UTF-8"?>

<Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android"


Android: layout_width = "fill_parent"


Android: layout_height = "fill_parent"


Android: paddingleft = "16dp"


Android: paddingright = "16dp">


<Edittext


Android: Id = "@ + ID/name"


Android: layout_width = "fill_parent"


Android: layout_height = "wrap_content"


Android: hint = "@ string/reminder"/>


<Spinner


Android: Id = "@ + ID/dates"


Android: layout_width = "0dp"


Android: layout_height = "wrap_content"


Android: layout_below = "@ ID/name"


Android: layout_alignparentleft = "true"


Android: layout_toleftof = "@ + ID/times"/>


<Spinner


Android: Id = "@ ID/times"


Android: layout_width = "96dp"


Android: layout_height = "wrap_content"


Android: layout_below = "@ ID/name"


Android: layout_alignparentright = "true"/>


<Button


Android: layout_width = "96dp"


Android: layout_height = "wrap_content"


Android: layout_below = "@ ID/times"


Android: layout_alignparentright = "true"


Android: text = "@ string/done"/>

</Relativelayout>

 

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.