需要动态改变RelativeLayout里面控件的相对位置,经一个技术群的群友提示,找到了如下的方法,做下记录:

  1.  
    RelativeLayout.Layoutparams params = (RelativeLayout.LayoutParams)控件1.getLayoutParams();
  2.  
    params.addRule(RelativeLayout.LEFT_OF, R.id.控件2);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//转载一个布局文件,要向这个布局汇总动态添加一个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);
//转载一个布局文件,要向这个布局汇总动态添加一个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);
04-15 02:31