public
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 確定界面的布局
AbsoluteLayout abslayout= new AbsoluteLayout ( this);
setContentView(abslayout);
// 創建一個button按鈕
Button btn1 = new Button( this);
btn1.setText(” this is a button”);
btn1.setId(1);
// 確定這個控件的大小和位置
AbsoluteLayout.LayoutParams lp1 =
new AbsoluteLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0,100);
abslayout.addView(btn1, lp1 );
}
super.onCreate(savedInstanceState);
// 確定界面的布局
AbsoluteLayout abslayout= new AbsoluteLayout ( this);
setContentView(abslayout);
// 創建一個button按鈕
Button btn1 = new Button( this);
btn1.setText(” this is a button”);
btn1.setId(1);
// 確定這個控件的大小和位置
AbsoluteLayout.LayoutParams lp1 =
new AbsoluteLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0,100);
abslayout.addView(btn1, lp1 );
}
一個界面可以布置一個布局,可以多個布局一起設計
public
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 設置界面的布局
RelativeLayout relativeLayout = new RelativeLayout( this);
setContentView(relativeLayout);
// 添加一個AbsoluteLayout子布局,並給這個布局添加一個button
AbsoluteLayout abslayout= new AbsoluteLayout ( this);
abslayout.setId(11);
Button btn1 = new Button( this);
btn1.setText(” this is a abslayout button”);
btn1.setId(1);
AbsoluteLayout.LayoutParams lp0 = new AbsoluteLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,100,0);
abslayout.addView(btn1, lp0 );
// 將這個子布局添加到主布局中
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
relativeLayout.addView(abslayout ,lp1);
// 再添加一個子布局
RelativeLayout relativeLayout1 = new RelativeLayout( this);
Button btn2 = new Button( this);
btn2.setText(” this is a relativeLayout1 button”);
btn2.setId(2);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp2.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
relativeLayout1.addView(btn2 ,lp2);
// 將這個布局添加到主布局中
RelativeLayout.LayoutParams lp11 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp11.addRule(RelativeLayout.BELOW ,11);
relativeLayout.addView(relativeLayout1 ,lp11);
}
super.onCreate(savedInstanceState);
// 設置界面的布局
RelativeLayout relativeLayout = new RelativeLayout( this);
setContentView(relativeLayout);
// 添加一個AbsoluteLayout子布局,並給這個布局添加一個button
AbsoluteLayout abslayout= new AbsoluteLayout ( this);
abslayout.setId(11);
Button btn1 = new Button( this);
btn1.setText(” this is a abslayout button”);
btn1.setId(1);
AbsoluteLayout.LayoutParams lp0 = new AbsoluteLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,100,0);
abslayout.addView(btn1, lp0 );
// 將這個子布局添加到主布局中
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
relativeLayout.addView(abslayout ,lp1);
// 再添加一個子布局
RelativeLayout relativeLayout1 = new RelativeLayout( this);
Button btn2 = new Button( this);
btn2.setText(” this is a relativeLayout1 button”);
btn2.setId(2);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp2.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
relativeLayout1.addView(btn2 ,lp2);
// 將這個布局添加到主布局中
RelativeLayout.LayoutParams lp11 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp11.addRule(RelativeLayout.BELOW ,11);
relativeLayout.addView(relativeLayout1 ,lp11);
}