Android relativelayout Attribute Usage details

Source: Internet
Author: User

Relative to the given ID control

The code is as follows Copy Code

Android:layout_above Place the bottom of the control over the control with the given ID;

Android:layout_below Place the bottom of the control under the control of the given ID;

Android:layout_toleftof aligns the right edge of the control with the left edge of the control with the given ID;

Android:layout_torightof aligns the left edge of the control with the right edge of the control with the given ID;

Android:layout_alignbaseline aligns the baseline of the control with the baseline of the given ID;

Android:layout_aligntop aligns the top edge of the control with the top edge of the given ID;

Android:layout_alignbottom aligns the bottom edge of the control with the bottom edge of the given ID;

Android:layout_alignleft aligns the left edge of the control with the left edge of the given ID;

Android:layout_alignright aligns the right edge of the control with the right edge of the given ID;

Relative to Parent component

Android:layout_alignparenttop If true, aligns the top of the control with the top of its parent control;

Android:layout_alignparentbottom If true, aligns the bottom of the control with the bottom of its parent control;

Android:layout_alignparentleft If true, aligns the left part of the control with the left part of its parent control;

Android:layout_alignparentright If true, aligns the right part of the control with the right part of its parent control;

Center

Android:layout_centerhorizontal If True, centers the control horizontally;

Android:layout_centervertical If True, centers the control vertically;

Android:layout_centerinparent If true, place the control in the center of the parent control;

External interval

The value of the offset on the android:layout_margintop;

Android:layout_marginbottom the value of the offset;

The value of the android:layout_marginleft left offset;

Android:layout_marginright the value of the right offset;

Relativelayout Code

The code is as follows Copy Code

Relativelayout relativelayout = new Relativelayout (context);
Spinner Spinner = new Spinner (context);
Edit_text_add_country = new Autocompletetextview (context);
Edit_text_add_stock = new EditText (this);
Radiogroup containcountry = new Radiogroup (context);


Spinner.setid (1);
Edit_text_add_country.setid (2);
Edit_text_add_stock.setid (3);
Containcountry.setid (4);

String[] Mcountries = {"We", "USA", "China2", "comunicate"};
list<string> allcountries = new arraylist<string> ();
Arrayadapter<string> aspncountries;

for (int i = 0; i < mcountries.length; i++) {
Allcountries.add (Mcountries[i]);
}

Aspncountries = new Arrayadapter<string> (This,
Android. R.layout.simple_spinner_item, allcountries);
Aspncountries
. Setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item);
Spinner.setadapter (aspncountries);
Edit_text_add_country.setadapter (aspncountries);
Edit_text_add_country.sethint ("Input country name");
Edit_text_add_stock.sethint ("Input stock name");

Relativelayout.layoutparams param1, param2, param3;
param1 = new Relativelayout.layoutparams (ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
param2 = new Relativelayout.layoutparams (ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Param2.addrule (Relativelayout.below, 1);
PARAM3 = new Relativelayout.layoutparams (ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Param3.addrule (Relativelayout.below, 4);
Viewgroup.layoutparams param4, PARAM5;
PARAM4 = new Viewgroup.layoutparams (viewgroup.layoutparams.fill_parent,viewgroup.layoutparams.wrap_content);
PARAM5 = new Viewgroup.layoutparams (viewgroup.layoutparams.fill_parent,viewgroup.layoutparams.wrap_content);

Containcountry.addview (SPINNER,PARAM4);
Containcountry.addview (Edit_text_add_country, PARAM5);

Relativelayout.addview (Containcountry, param1);
Relativelayout.addview (Edit_text_add_stock, param3);

Builder.setview (relativelayout);

Look at an example

Relativelayout code implementation and click Settings

The code is as follows Copy Code

Public class Main extends activity {
  
    @Override
    Public v OID onCreate (Bundle savedinstancestate) {
        super.oncreate ( Savedinstancestate);
        Setcontentview (R.layout.main);
      
        arraylist<person> Personlist = new arraylist<person> ();
        Personlist.add (New person ("zwx", "23"));
        Personlist.add (New person ("zwx1", "24"));
        Personlist.add (New person ("zwx2", "25"));
      
        linearlayout ll = ( LinearLayout) Findviewbyid (r.id.linearlayout01);


for (person Person:personlist) {
Relativelayout RL = new Relativelayout (this);
Relativelayout.layoutparams RLL = new Relativelayout.layoutparams (LAYOUTPARAMS.FILL_PARENT,LAYOUTPARAMS.WRAP_ CONTENT);
Rl.setlayoutparams (RLL);


TextView TV1 = new TextView (this);
Relativelayout.layoutparams tv1_rl = new Relativelayout.layoutparams (LAYOUTPARAMS.WRAP_CONTENT,LAYOUTPARAMS.WRAP_ CONTENT);
Tv1_rl.addrule (Relativelayout.align_parent_left);
Tv1.settext (Person.getname ());
Rl.addview (TV1, TV1_RL);

TextView TV2 = new TextView (this);
Relativelayout.layoutparams tv2_rl = new Relativelayout.layoutparams (LAYOUTPARAMS.WRAP_CONTENT,LAYOUTPARAMS.WRAP_ CONTENT);
Tv2_rl.addrule (Relativelayout.align_parent_right);
Tv2.settext (Person.getage ());
Rl.addview (TV2, TV2_RL);


          Ll.addview (RL);
           Rl.setonclicklistener (New View.onclicklistener () {
    
                 @Override
               public void OnClick (View v) {
                    Toast.maketext (Main.this, "one", 5000). Show ();
               }
              }) ;
       }
   }
}


Note: It is not possible to generate a circular dependency between the Relativelayout container itself and his child elements, for example, if the high setting of the relativelayout is not wrap_content, the height of the child element is set to Align_parent _bottom.

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.