可以直接newView来得到View对象来实现代码布局。以下为示例代码:1.绝对布局AbsoluteLayoutabslayout=newAbsoluteLayout(this);setContentView(abslayout);Buttonbtn1=newButton(this);btn1.setText(”thisisabutton”);btn1.setId(1);AbsoluteLayout.LayoutParamslp1=newAbsoluteLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,0,100);abslayout.addView(btn1,lp1);2.相对布局RelativeLayoutrelativeLayout=newRelativeLayout(this);setContentView(relativeLayout);AbsoluteLayoutabslayout=newAbsoluteLayout(this);RelativeLayout.LayoutParamslp1=newRelativeLayout.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);3.线性布局LinearLayoutll=newLinearLayout(this);EditTextet=newEditText();ll.addView(et);//动态添加布局的方法1.LinearLayoutll=(LinearLayout)this.getLayoutInflater().inflate(R.layout.main1,null);setContentView(ll);LinearLayoutll2=(LinearLayout)this.getLayoutInflater().inflate(R.layout.main2,ll);//这样main2作为main1的子布局加到了main1的根节点下//动态添加布局的方法2addView.LinearLayoutll=(LinearLayout)this.getLayoutInflater().inflate(R.layout.main1,null);setContentView(ll);LinearLayoutll2=(LinearLayout)this.getLayoutInflater().inflate(R.layout.main2,null);ll.addView(ll2);