一、基本步骤
1、定义一个RelativeLayout对象
- private RelativeLayout rlActivityMain;
2、定义一个EditText对象并将其初始化
- private EditText etIntroductImage;
- etIntroductImage=new EditText(MainActivity.this);
- etIntroductImage.setText("图片说明");
3、定义一个RelativeLayout.LayoutParams对象并将其初始化
- private RelativeLayout.LayoutParams tempLayoutParams;
- tempLayoutParams=new RelativeLayout.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
- package com.weipeng.adroid.myrelativelayout;
-
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- 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.widget.RelativeLayout.LayoutParams;
- import android.widget.TextView;
-
- public class MainActivity extends Activity implements OnClickListener {
-
-
-
- private Button btnUp;
- private Button btnDown;
- private Button btnLeft;
- private Button btnRight;
-
- int[] Buttons;
-
- private ImageView imgFengJing;
-
- private EditText etIntroductImage;
-
- private RelativeLayout rlActivityMain;
-
- private RelativeLayout.LayoutParams tempLayoutParams;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- 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");
-
-
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
-
- @Override
- public void onClick(View v) {
-
-
- etIntroductImage=new EditText(MainActivity.this);
- etIntroductImage.setText("图片说明");
- tempLayoutParams=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 90);
-
- switch (v.getId()) {
- case R.id.btnUp:
- tempLayoutParams.addRule(RelativeLayout.ABOVE, imgFengJing.getId());
- tempLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL,imgFengJing.getId());
- rlActivityMain.addView(etIntroductImage, tempLayoutParams);
- break;
- case R.id.btnDown:
- tempLayoutParams.addRule(RelativeLayout.BELOW, imgFengJing.getId());
- tempLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, imgFengJing.getId());
- rlActivityMain.addView(etIntroductImage, tempLayoutParams);
- break;
- case R.id.btnLeft:
- tempLayoutParams.addRule(RelativeLayout.LEFT_OF,imgFengJing.getId());
- tempLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, imgFengJing.getId());
- rlActivityMain.addView(etIntroductImage, tempLayoutParams);
- break;
- case R.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
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/rlActivityMain"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#ffa500" >
-
- <Button
- android:id="@+id/btnUp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:text="@string/上" />
-
- <Button
- android:id="@+id/btnDown"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_centerHorizontal="true"
- android:text="@string/下" />
-
- <Button
- android:id="@+id/btnLeft"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_centerVertical="true"
- android:text="@string/左" />
-
- <Button
- android:id="@+id/btnRight"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_centerVertical="true"
- android:text="@string/右" />
-
- <ImageView
- android:id="@+id/imgFengJing"
- android:layout_width="33dp"
- android:layout_height="33dp"
- android:layout_centerInParent="true"
- android:src="@drawable/fengjing" />
-
- </RelativeLayout>