一、基本步骤
1、定义一个RelativeLayout对象
privateRelativeLayoutrlActivityMain;
2、定义一个EditText对象并将其初始化
privateEditTextetIntroductImage;
etIntroductImage=newEditText(MainActivity.this);etIntroductImage.setText("图片说明");
3、定义一个RelativeLayout.LayoutParams对象并将其初始化
privateRelativeLayout.LayoutParamstempLayoutParams;
tempLayoutParams=newRelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,90);//ViewGroup.LayoutParams.WRAP_CONTENT是要添加控件的width属性的值,90是要添加控件的height属性的值
4、设置EditText控件的位置
//imgFengJing的定义为private ImageView imgFengJing;//将EditText放在imgFengJing上方水平居中的位置tempLayoutParams.addRule(RelativeLayout.ABOVE,imgFengJing.getId());tempLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL,imgFengJing.getId());
5、将EditText添加到布局中
rlActivityMain.addView(etIntroductImage,tempLayoutParams);
二、效果图
三、源代码
MainActivity.java
packagecom.weipeng.adroid.myrelativelayout;importandroid.os.Bundle;importandroid.app.Activity;importandroid.view.Menu;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.view.ViewGroup;importandroid.widget.Button;importandroid.widget.EditText;importandroid.widget.ImageView;importandroid.widget.RelativeLayout;importandroid.widget.RelativeLayout.LayoutParams;importandroid.widget.TextView;publicclassMainActivityextendsActivityimplementsOnClickListener{privateButtonbtnUp;privateButtonbtnDown;privateButtonbtnLeft;privateButtonbtnRight;int[]Buttons;privateImageViewimgFengJing;privateEditTextetIntroductImage;privateRelativeLayoutrlActivityMain;privateRelativeLayout.LayoutParamstempLayoutParams;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);System.out.println("已执行:setContentView");btnUp=(Button)findViewById(R.id.btnUp);btnDown=(Button)findViewById(R.id.btnDown);btnLeft=(Button)findViewById(R.id.btnLeft);btnRight=(Button)findViewById(R.id.btnRight);System.out.println("已执行:(Button) findViewById");rlActivityMain=(RelativeLayout)findViewById(R.id.rlActivityMain);imgFengJing=(ImageView)findViewById(R.id.imgFengJing);System.out.println("已执行:(RelativeLayout) findViewById");System.out.println("已执行:(ImageView) findViewById");btnUp.setOnClickListener(this);btnDown.setOnClickListener(this);btnLeft.setOnClickListener(this);btnRight.setOnClickListener(this);System.out.println("已执行:setOnClickListener");}@OverridepublicbooleanonCreateOptionsMenu(Menumenu){getMenuInflater().inflate(R.menu.main,menu);returntrue;}@OverridepublicvoidonClick(Viewv){etIntroductImage=newEditText(MainActivity.this);etIntroductImage.setText("图片说明");tempLayoutParams=newRelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,90);switch(v.getId()){caseR.id.btnUp:tempLayoutParams.addRule(RelativeLayout.ABOVE,imgFengJing.getId());tempLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL,imgFengJing.getId());rlActivityMain.addView(etIntroductImage,tempLayoutParams);break;caseR.id.btnDown:tempLayoutParams.addRule(RelativeLayout.BELOW,imgFengJing.getId());tempLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL,imgFengJing.getId());rlActivityMain.addView(etIntroductImage,tempLayoutParams);break;caseR.id.btnLeft:tempLayoutParams.addRule(RelativeLayout.LEFT_OF,imgFengJing.getId());tempLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL,imgFengJing.getId());rlActivityMain.addView(etIntroductImage,tempLayoutParams);break;caseR.id.btnRight:tempLayoutParams.addRule(RelativeLayout.RIGHT_OF,imgFengJing.getId());tempLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL,imgFengJing.getId());rlActivityMain.addView(etIntroductImage,tempLayoutParams);break;}System.out.println("已执行:onClick");}}
activity_main_xml