Android 动态设置控件的布局
在android开发中,常常会动态地生成些控件,并调调整其布局。那么,如何动态调整空间的布局,请参阅下面的关键代码:
?
?
mRlMain = (RelativeLayout) findViewById(R.id.rlMain);????
LayoutParams layoutParams = new LayoutParams(?
??? android.view.ViewGroup.LayoutParams.WRAP_CONTENT,?
??? android.view.ViewGroup.LayoutParams.WRAP_CONTENT);???
mBtnView = LayoutInflater.from(mContext).inflate(R.layout.photoright, null);?
layoutParams.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);?
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);?????
mRlMain.addView(mBtnView, layoutParams);?
?主要用到的类有:android.widget.RelativeLayout.LayoutParams,android.view.ViewGroup.LayoutParams及android.widget.RelativeLayout
?
上面的主要是用相对布局设置,其他布局方式类似。