//轉載一個布局文件,要向這個布局匯總動態添加一個button
RelativeLayout relativeLayout(RelativeLayout)getLayoutInflater().inflate(R.layout.main,null); //裝載要動態添加的按鈕布局 Button button = (Button)getLayoutInflater().inflate(R.layout.button,null); //創建一個LayoutParams對象 RelativeLayoutLayoutParamslayoutParams= new RelativeLayout。LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); layoutParams.addRule(RelativeLayout.RIGHT_OF,R.id.button1); layoutParams.addRule(RelativeLayout.BELOW,R.id.button); button.setLayoutParams(layoutParams); relativeLayout.addView(button);