在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
上面的主要是用相對布局設置,其他布局方式類似。