使用LayoutInflater類的inflate函數,將第一個參數的布局加載到第二個參數的布局上。
第一個參數是個int對應一個布局文件,第二個參數是個View對象實例。
getMenuInflater().inflate(R.menu.main, layout);
獲取已經加載的組件對象實例:
Button button = (Button)findViewById(R.id.button);
創建一個新的線性布局對象實例:
LinearLayout layout = new LinearLayout(context);
創建空的動態線性布局並動態添加按鈕並在活動中顯示:
LinearLayout layout = new LinearLayout(this); //創建空的動態線性布局對象,傳入參數為context
Button button = new Button(this); //創建一個空按鈕,參數同上
layout.addView(button); //添加按鈕實例到布局
setContentView(layout); //添加布局到當前活動