Android basics: relative layout manager RelativeLayout

Source: Internet
Author: User

Relative layout manager is a layout manager based on a reference point. Just like the relative path concept in Web development, it is created based on a certain reference point. In Android, the relative layout manager is the layout manager on the top, bottom, left, right of a reference point.

The following describes the document of RelativeLayout:

Its Inheritance structure is:

 
 
  1. java.lang.Object 
  2.    ↳ android.view.View 
  3.    ↳ android.view.ViewGroup 
  4.    ↳ android.widget.RelativeLayout 

Next, we will create a new project in Eclipse to see how to use the relative layout manager RelativeLayout:

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent" 
  5.     android:orientation="vertical" > 
  6.     <ImageView 
  7.         android:id="@+id/img1" 
  8.         android:layout_width="wrap_content" 
  9.         android:layout_height="wrap_content" 
  10.         android:src="@drawable/ic_launcher" /> 
  11.     <ImageView 
  12.         android:id="@+id/img2" 
  13.         android:layout_width="wrap_content" 
  14.         android:layout_height="wrap_content" 
  15.         android:src="@drawable/google_plus" 
  16.         android:layout_toRightOf="@+id/img1" /> 
  17. </RelativeLayout> 

In main. in xml, the layout manager is declared as RelativeLayout, and two ImageView components are created to display two images. The layout_toRightOf attribute is set on the second ImageView component, that is to say, set the value of the component ID relative to the right of a component. In this case, the position of img2 relative to img1 is the right. Run the program below and we can see the following results:

Obviously, the second image is placed on the right of the first image, and a TextView component is added to the Code below:

 
 
  1. <TextView android: id = "@ + id/txt"
  2. Android: layout_width = "wrap_content"
  3. Android: layout_height = "wrap_content"
  4. Android: text = "some text is displayed here"
  5. Android: layout_below = "@ + id/img2"/>

This component is also very simple. We have set the layout_below attribute, which indicates that it should be placed under the second image. Then, run the program and we will get the following display results:

No problem. The text is indeed below the second image, but it is displayed at the top. If the first image is smaller than the second one, it will produce a coverage effect, let's take a look at the adjusted position. The adjusted code is:

 
 
  1. <ImageView
  2. Android: id = "@ + id/img1"
  3. Android: layout_width = "wrap_content"
  4. Android: layout_height = "wrap_content"
  5. Android: src = "@ drawable/ic_launcher"
  6. Android: layout_toRightOf = "@ + id/img2"/>
  7. <ImageView
  8. Android: id = "@ + id/img2"
  9. Android: layout_width = "wrap_content"
  10. Android: layout_height = "wrap_content"
  11. Android: src = "@ drawable/google_plus"/>
  12. <TextView android: id = "@ + id/txt"
  13. Android: layout_width = "wrap_content"
  14. Android: layout_height = "wrap_content"
  15. Android: text = "some text is displayed here"
  16. Android: layout_below = "@ + id/img1"/>

Here we will not explain the meaning of the code and run it directly. We can see that:

If the first image is displayed, you need to set it again:

 
 
  1. <TextView android: id = "@ + id/txt"
  2. Android: layout_width = "wrap_content"
  3. Android: layout_height = "wrap_content"
  4. Android: text = "some text is displayed here"
  5. Android: layout_below = "@ + id/img1"
  6. Android: layout_toRightOf = "@ + id/img2"/>

Run the program again and we can see the following results:

The text is under img1 and on the right of img2. At this time, there is space on the lower side of the text and on the right side of img2. We will place another Button component:

 
 
  1. <Button
  2. Android: id = "@ + id/btn"
  3. Android: layout_width = "wrap_content"
  4. Android: layout_height = "wrap_content"
  5. Android: text = "button"
  6. Android: layout_below = "@ + id/txt"
  7. Android: layout_toRightOf = "@ + id/img2"/>

Run the program again and we will get the following results:

Like other layout managers, we can use Java code to control the relative layout manager. The following describes the document of RelativeLayout. LayoutParams:

Its Inheritance structure is:

 
 
  1. java.lang.Object 
  2.    ↳ android.view.ViewGroup.LayoutParams 
  3.    ↳ android.view.ViewGroup.MarginLayoutParams 
  4.    ↳ android.widget.RelativeLayout.LayoutParams  

You only need to set some rules when controlling the relative layout manager in the code, that is, the layout_toRightOf and layout_below shown above. Let's take a look at the code below:

 
 
  1. Package org. ourpioneer;
  2. Import android. app. Activity;
  3. Import android. OS. Bundle;
  4. Import android. view. ViewGroup;
  5. Import android. widget. EditText;
  6. Import android. widget. RelativeLayout;
  7. Public class RelativeLayoutDemoActivity extends Activity {
  8. @ Override
  9. Public void onCreate (Bundle savedInstanceState ){
  10. Super. onCreate (savedInstanceState );
  11. Super. setContentView (R. layout. main); // read the existing layout manager
  12. RelativeLayout relativeLayout = (RelativeLayout) super
  13. . FindViewById (R. id. rLayout); // obtain the relative layout manager rLayout
  14. RelativeLayout. LayoutParams params = new RelativeLayout. LayoutParams (
  15. ViewGroup. LayoutParams. FILL_PARENT,
  16. ViewGroup. LayoutParams. FILL_PARENT); // you can specify the parameters of the layout manager.
  17. Params. addRule (RelativeLayout. BELOW, R. id. btn); // you can specify a placement rule.
  18. EditText edit = new EditText (this); // create the EditText component
  19. RelativeLayout. addView (edit, params );
  20. }
  21. }

Before writing code, we need. in xml, add the ID attribute (rLayout) for our layout manager. Then we can control it in the Code. Here, we add components in the existing layout manager, that is, to place an edit box under the button, we set the layout manager parameters to FILL_PARENT, that is, to fill the entire screen, and then the rule is located at the lower side of btn, then add the component to the layout manager and run the program. We can see that:

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.