在RelativeLayout佈局中能夠設置標籤的android:layout_toLeftO...

public class AndrodTActivity extends Activity {
 //在RelativeLayout佈局中能夠設置標籤的android:layout_toLeftOf,android:layout_toRightOf等屬性,如何用Java代碼完成這些工做。
 LayoutInflater inflater;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  inflater = LayoutInflater.from(this);
  // 裝載一個佈局文件,要向這個佈局中動態添加一個Button
  RelativeLayout rl = (RelativeLayout) inflater.inflate(R.layout.main, null);
  // 裝載要動態添加的按鈕佈局
  Button button2 = (Button) inflater.inflate(R.layout.button2, null);
  // 建立一個LayoutParams 對象
  RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  // addRule方法 將按鈕佈局添加到button1的右邊
  params.addRule(RelativeLayout.RIGHT_OF, R.id.button1);
  // 更新將要添加按鈕的屬性值
  button2.setLayoutParams(params);
  rl.addView(button2);
  setContentView(rl);
 } android

} ide

相關文章
相關標籤/搜索