The parameters of the Android relalivelayout layout are defined:
Android:layout_above= "@id/xxx"-puts the control on top of the given ID control
android:layout_below= "@id/xxx"-places the control under the given ID control
android:layout_toleftof= "@id/xxx"-Aligns the right edge of the control with the left edge of the given ID control
android:layout_torightof= "@id/xxx"-aligns the left edge of the control with the right edge of the given ID control
android:layout_alignleft= "@id/xxx"-aligns the left edge of the control with the left edge of the given ID control
android:layout_aligntop= "@id/xxx"-aligns the top edge of the control with the top edge of the given ID control
android:layout_alignright= "@id/xxx"-Aligns the right edge of the control with the right edge of the given ID control
android:layout_alignbottom= "@id/xxx"-Aligns the bottom edge of the control with the bottom edge of the given ID control
Android:layout_alignparentleft= "true"-aligns the left edge of the control with the left edge of the parent control
Android:layout_alignparenttop= "true"-aligns the top edge of the control with the top edge of the parent control
Android:layout_alignparentright= "true"-aligns the right edge of the control with the right edge of the parent control
Android:layout_alignparentbottom= "true"-aligns the bottom edge of the control with the bottom edge of the parent control
Android:layout_centerinparent= "true"--places the control in the center of the parent control
Android:layout_centerhorizontal= "true"--places the control in the center of the horizontal direction
Android:layout_centervertical= "true"--places the control in the center of the vertical direction
Programming Examples:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/49/15/wKioL1QOiKLTYaOTAABrRMsrBfg792.jpg "title=" Relative.jpg "alt=" Wkiol1qoikltyaotaabrrmsrbfg792.jpg "/>
Define Layout Activity_main.xml
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/r1"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:background= "#edab4a" >
<button
Android:id= "@+id/shang"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_alignparenttop= "true"
Android:layout_centerhorizontal= "true"
android:text= "@string/shang"/>
<button
Android:id= "@+id/xia"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_alignparentbottom= "true"
Android:layout_centerhorizontal= "true"
android:text= "@string/xia"/>
<button
Android:id= "@+id/zuo"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_alignparentleft= "true"
Android:layout_centervertical= "true"
android:text= "@string/zuo"/>
<button
Android:id= "@+id/you"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_alignparentright= "true"
Android:layout_centervertical= "true"
android:text= "@string/you"/>
<imagebutton
android:contentdescription= "@string/wutu"
Android:id= "@+id/start"
Android:layout_width= "60dip"
android:layout_height= "100dip"
Android:layout_centerinparent= "true"
android:src= "@drawable/fengjing"/>
</RelativeLayout>
string Resource Sring.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<string name= "App_name" >RelativeLayout</string>
<string name= "Action_settings" >Settings</string>
<string name= "Hello_world" >hello world!</string>
<string name= "Shang" > on </string>
<string name= "Xia" > Under </string>
<string name= "Zuo" > Left </string>
<string name= "You" > right </string>
<string name= "Wutu" > No diagram </string>
<string name= "shuoming" > Picture description </string>
</resources>
Main activity Mainactivity.java
Package com.malakana.relativelayout;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.ViewGroup;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.ImageView;
Import Android.widget.RelativeLayout;
Import android.app.Activity;
public class Mainactivity extends Activity {
Relativelayout R1;
Button Shang;
Button Xia;
Button Zuo;
Button you;
ImageView Currbutton;
ImageView start;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
R1 = (relativelayout) Findviewbyid (R.ID.R1);
Shang = (Button) Findviewbyid (R.id.shang);
Xia = (Button) Findviewbyid (R.id.xia);
Zuo = (Button) Findviewbyid (R.id.zuo);
you = (Button) Findviewbyid (r.id.you);
Start = (ImageView) Findviewbyid (R.id.start);
Currbutton = start;
Shang.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View arg0) {
TODO auto-generated Method Stub
EditText temp = new EditText (mainactivity.this);
Temp.settext (r.string.shuoming);
Set Control position
Relativelayout.layoutparams lp_1 =
New Relativelayout.layoutparams (
viewgroup.layoutparams.wrap_content,95);
Lp_1.addrule (Relativelayout.above,currbutton.getid ()); ABOVE above the Currbutton
Lp_1.addrule (Relativelayout.center_horizontal,currbutton.getid ());
To add a control to a layout
R1.addview (temp,lp_1);
}});
Xia.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View arg0) {
TODO auto-generated Method Stub
EditText temp = new EditText (mainactivity.this);
Temp.settext (r.string.shuoming);
Set Control position
Relativelayout.layoutparams lp_1 =
New Relativelayout.layoutparams (
viewgroup.layoutparams.wrap_content,95);
Lp_1.addrule (Relativelayout.below,currbutton.getid ()); BELOW under the Currbutton
Lp_1.addrule (Relativelayout.center_horizontal,currbutton.getid ());
To add a control to a layout
R1.addview (temp,lp_1);
}});
Zuo.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View arg0) {
TODO auto-generated Method Stub
EditText temp = new EditText (mainactivity.this);
Temp.settext (r.string.shuoming);
Set Control position
Relativelayout.layoutparams lp_1 =
New Relativelayout.layoutparams (
viewgroup.layoutparams.wrap_content,95);
Lp_1.addrule (Relativelayout.left_of,currbutton.getid ()); Left_of aligns the right edge of the control and the left edge of the Currbutton
Lp_1.addrule (Relativelayout.center_vertical,currbutton.getid ()); Place the control in the center of the vertical direction
To add a control to a layout
R1.addview (temp,lp_1);
}});
You.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View arg0) {
TODO auto-generated Method Stub
EditText temp = new EditText (mainactivity.this);
Temp.settext (r.string.shuoming);
Set Control position
Relativelayout.layoutparams lp_1 =
New Relativelayout.layoutparams (
viewgroup.layoutparams.wrap_content,95);
Lp_1.addrule (Relativelayout.right_of,currbutton.getid ()); Right_of aligns the left edge of the control with the right edge of the Currbutton
Lp_1.addrule (Relativelayout.center_vertical,currbutton.getid ()); Place the control in the center of the vertical direction
To add a control to a layout
R1.addview (temp,lp_1);
}});
}
}
:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/49/17/wKiom1QOvS-y8hWUAACQjBNg40U279.jpg "title=" Relativelayout.jpg "alt=" Wkiom1qovs-y8hwuaacqjbng40u279.jpg "/>
Relativelayout of Android interface layout