AbsoluteLayout.LayoutParams absoluteParams1 = (AbsoluteLayout.LayoutParams) btn.getLayoutParams(); absoluteParams1.x = x; absoluteParams1.y = y; btn.setLayoutParams(absoluteParams1) //绝对布局 AbsoluteLayout abslayout=new AbsoluteLayout (this); setContentView(abslayout); Button btn1 = new Button(this); btn1.setText(”this is a button”); btn1.setId(1); AbsoluteLayout.LayoutParams lp1 = new AbsoluteLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0,100); abslayout.addView(btn1, lp1); //相对布局 RelativeLayout relativeLayout = new RelativeLayout(this); setContentView(relativeLayout); AbsoluteLayout abslayout=new AbsoluteLayout (this); RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP); lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); relativeLayout.addView(abslayout ,lp1); //线性布局 LinearLayout ll = new LinearLayout(this); EditText et = new EditText(); ll.addView(et); //动态添加布局的方法1. LinearLayout ll = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main1,null); setContentView(ll); LinearLayout ll2 = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main2,ll); //这样 main2 作为 main1的子布局 加到了 main1的 根节点下 //动态添加布局的方法2 addView. LinearLayout ll = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main1,null); setContentView(ll); LinearLayout ll2 = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main2,null); ll.addView(ll2);Toast.makeText(getApplicationContext(), "x"+x+"y"+y, Toast.LENGTH_SHORT).show(); 让你的activity 继承onclicklisten。然后每个button都设置this 作为监听器。activity的实现方法通过view的ID区别 package jp.javadrive.android; import android.app.Activity; import android.os.Bundle; import android.widget.LinearLayout; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.view.View.OnClickListener; public class Test04_01 extends Activity implements OnClickListener{ private final int WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT; private int count1; private int count2; private Button button1; private Button button2; @Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); count1 = 0; count2 = 0; LinearLayout linearLayout = new LinearLayout(this); linearLayout.setOrientation(LinearLayout.HORIZONTAL); setContentView(linearLayout); button1 = new Button(this); button1.setText("Count"); button1.setOnClickListener(this); linearLayout.addView(button1, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT)); button2 = new Button(this); button2.setText("Count"); button2.setOnClickListener(this); linearLayout.addView(button2, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT)); } public void onClick(View v) { if (v == button1){ count1++; button1.setText("Count:" + count1); }else if (v == button2){ count2++; button2.setText("Count:" + count2); } } }