Android 0 Basics Section 28th: Easy to master relativelayout relative layout

Source: Internet
Author: User
Tags xml attribute

In the previous three period we have detailed analysis of LinearLayout, LinearLayout is also a more of the layout we use. However, in the actual development of the use of LinearLayout is far from enough, we have to learn relativelayout together this period.

I. Understanding Relativelayout

Relativelayout, also known as relative layout, uses relativelayout tags. Relative layouts usually have two forms, one relative to the container and one relative to the control.

The following table shows the common XML attributes supported by Relativelayout and the description of related methods.

To control the layout distribution of the sub-components in the layout container, Relativelayout provides an inner class: Relativelayout.layoutparams, which provides a large number of XML properties to control the layout distribution of sub-components in a relativelayout layout container.

The properties that are positioned relative to the container are mainly the following, with a property value of true or false.

    • Android:layout_centerhorizontal: Controls whether the component is centered horizontally with the layout container.

    • Android:layout_centervertical: Controls whether the component is centered vertically with the layout container.

    • Android:layout_centerinparent: Controls whether the component and the layout container's central location.

    • Android:layout_alignparenttop: Controls whether the component aligns with the top of the layout container.

    • Android:layout_alignparentbottom: Controls whether the component aligns with the bottom of the layout container.

    • Android:layout_alignparentleft: Controls whether the component aligns to the left of the layout container.

    • Android:layout_alignparentright: Controls whether the component aligns to the right side of the layout container.

    • Android:layout_alignparentstart: Controls whether the component aligns with the start of the layout container.

    • Android:layout_alignparentend: Controls whether the component aligns with the end of the layout container.

    • Android:layout_alignwithparentifmissing: If the corresponding sibling component is not found, the parent container is the reference.

There are several main properties that are positioned relative to other components, and the property value is the ID of the other component.

    • Android:layout_toleftof: This component is on the left side of a component.

    • Android:layout_torightof: This component is on the right side of a component.

    • Android:layout_tostartof: This component is at the beginning of a component.

    • Android:layout_toendof: This component is at the end of a component.

    • Android:layout_above: This component is above a component.

    • Android:layout_below: This component is below a component.

    • Android:layout_alignbaseline: This component aligns with the baseline of a component.

    • Android:layout_aligntop: The top of this component is aligned with the top of a component.

    • Android:layout_alignbottom: The lower edge of this component is aligned with the lower edge of a component.

    • Android:layout_alignright: The right edge of this component is aligned with the right edge of a component.

    • Android:layout_alignleft: Aligns the left edge of this component with the left edge of a component.

    • Android:layout_alignstart: The start side of this component is aligned with the beginning of a component.

    • Android:layout_alignend: The end of this component is aligned with the end of a component.

In addition, Relativelayout.layoutparams also inherited the Android view. Viewgroup.marginlayoutparams, so each sub-component in the Relativelayout layout container can also be specified Each XML attribute supported by the Android.view.ViewGroiip.MarginLayoutParams.

Ii. examples

Next, learn the usage of relativelayout using a simple example program.

Continue to use the Activity_main.xml file in the app/main/res/layout/directory, where the following code fragment is populated:

<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <!--defines that the component is located on the upper left side of the parent container -    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "upper left side of container"Android:layout_alignparentleft= "true"Android:layout_alignparenttop= "true"/>    <!--defines that the component is centered horizontally on the parent container side -    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Horizontal center on the top side of the container"Android:layout_alignparenttop= "true"Android:layout_centerhorizontal= "true"/>    <!--defines that the component is located on the upper right side of the parent container -    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "upper Right side of container"Android:layout_alignparentright= "true"Android:layout_alignparenttop= "true"/>    <!--defines that the component is centered vertically on the left side of the parent container -    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Left Middle"Android:layout_alignparentleft= "true"android:layout_centervertical= "true"/>    <!--defines that the component is centered vertically on the right side of the parent container -    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Right Middle"Android:layout_alignparentright= "true"android:layout_centervertical= "true"/>    <!--defines that the component is located in the lower left side of the parent container -    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "lower left side of container"Android:layout_alignparentleft= "true"Android:layout_alignparentbottom= "true"/>    <!--defines that the component is centered horizontally below the parent container -    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Horizontal center of the lower side of the container"Android:layout_alignparentbottom= "true"Android:layout_centerhorizontal= "true"/>    <!--defines that the component is located on the lower right side of the parent container -    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "lower right side of container"Android:layout_alignparentright= "true"Android:layout_alignparentbottom= "true"/>    <!--defines the component in the middle of the parent container -    <ButtonAndroid:id= "@+id/center_btn"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Central container"android:layout_centerinparent= "true"/>    <!--defines that the component is located above the Center_btn component -    <ButtonAndroid:id= "@+id/center_top_btn"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Upper -Middle"Android:layout_above= "@id/center_btn"Android:layout_alignleft= "@id/center_btn"/>    <!--defines that the component is located below the Center_btn component -    <ButtonAndroid:id= "@+id/center_bottom_btn"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Lower"Android:layout_below= "@id/center_btn"Android:layout_alignleft= "@id/center_btn"/>    <!--defines that the component is on the left side of the CENTER_BTN component -    <ButtonAndroid:id= "@+id/center_bottom_left_btn"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "lower left"Android:layout_toleftof= "@id/center_btn"Android:layout_aligntop= "@id/center_bottom_btn"/>    <!--defines that the component is located on the right side of the CENTER_BTN component -    <ButtonAndroid:id= "@+id/center_top_right_btn"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Top Right"Android:layout_torightof= "@id/center_btn"Android:layout_aligntop= "@id/center_top_btn"/></Relativelayout>

To run the program, you can see the interface effect shown:

To this end of the relativelayout example, more usage of relativelayout can refer to the XML properties and methods reference table above, suggesting a hands-on exercise.

Come here today, if you have any questions welcome message to discuss together, also welcome to join the Android 0 Basic introductory Technical discussion group, grow together!

This article copyright for the public Share talent show (Shareexpert)--Xin 鱻 all, if reproduced please note source, hereby declare!

Past period Summary share:

Android 0 Basics Introduction 1th: Android's past life

Android 0 Basics Section 2nd: Android system Architecture and application components those things

Android 0 Basics Section 3rd: Bring you up to talk about Android development environment

Android 0 Basics 4th: Installing and configuring the JDK correctly Ko fu the first trick

Android 0 Basics 5th: Use ADT bundles to easily meet the goddess

Android 0 Basics 6th: Configuration Optimization SDK Manager, official dating goddess

Android 0 Basics 7th: Take care of Android simulator and start the Sweet journey

Android 0 Basics 8th: HelloWorld, the starting point for my first trip

Android 0 Basics 9th: Android app, no code can be developed

Android 0 Basics Section 10th: Development IDE Big upgrade, finally ushered in Android Studio

Android 0 Basics Introductory Section 11th: Simple steps to take you to fly, run Android Studio project

Android 0 Basics 12th: Get familiar with the Android studio interface and start selling

Android 0 Basics 13th: Android Studio Configuration optimization to create a development tool

Android 0 Basics 14th: Using high-speed genymotion, stepping into the rocket era

Android 0 Basics Section 15th: Mastering the Android Studio project structure, sailing

Android 0 Basics Section 16th: Android User Interface Development overview

Android 0 Basics Section 17th: TextView Properties and Methods Daquan

Android 0 Basics Section 18th: EditText properties and how to use them

Android 0 Basics section 19th: Button usage explained

Android 0 Basics Section 20th: checkbox and RadioButton Usage Daquan

Android 0 Basics Section 21st: ToggleButton and switch usage Daquan

Android 0 Basics Section 22nd: ImageView's properties and methods Daquan

Android 0 Basics Section 23rd: ImageButton and Zoombutton use Daquan

Android 0 Basics Section 24th: Customize view simple to use to create your own controls

Android 0 Basics Section 25th: Simple and most commonly used linearlayout linear layouts

Android 0 Basics Section 26th: Two alignments, layout_gravity and gravity differ greatly

Android 0 Basics section 27th: Using padding and margin correctly

Android 0 Basics Section 28th: Easy to master relativelayout relative layout

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.