在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
上面的主要是用相對佈局設置,其他佈局方式類似。
摘自 心靈凈土的專欄