Android 动态设置控件的布局


在Android开发中,常常会动态地生成些控件,并调调整其布局。那么,如何动态调整空间的布局,请参阅下面的关键代码:

  1. mRlMain = (RelativeLayout) findViewById(R.id.rlMain);     
  2. LayoutParams layoutParams = new LayoutParams(  
  3.     android.view.ViewGroup.LayoutParams.WRAP_CONTENT,  
  4.     android.view.ViewGroup.LayoutParams.WRAP_CONTENT);    
  5. mBtnView = LayoutInflater.from(mContext).inflate(R.layout.photoright, null);  
  6. layoutParams.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);  
  7. layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);      
  8. mRlMain.addView(mBtnView, layoutParams);  

主要用到的类有:android.widget.RelativeLayout.LayoutParams,android.view.ViewGroup.LayoutParams及android.widget.RelativeLayout

上面的主要是用相对布局设置,其他布局方式类似。

相关内容