01 | public void onCreate(Bundle savedInstanceState) { |
02 | super .onCreate(savedInstanceState); |
03 | //确定界面的布局 |
04 | AbsoluteLayout abslayout= new AbsoluteLayout ( this ); |
05 | setContentView(abslayout); |
06 | //创建一个button按钮 |
07 | Button btn1 = new Button( this ); |
08 | btn1.setText(” this is a button”); |
09 | btn1.setId( 1 ); |
10 | //确定这个控件的大小和位置 |
11 | AbsoluteLayout.LayoutParams lp1 = |
12 | new AbsoluteLayout.LayoutParams( |
13 | ViewGroup.LayoutParams.WRAP_CONTENT, |
14 | ViewGroup.LayoutParams.WRAP_CONTENT, |
15 | 0 , 100 ); |
16 | abslayout.addView(btn1, lp1 ); |
17 |
18 | } |
一个界面可以布置一个布局,可以多个布局一起设计
01 | public void onCreate(Bundle savedInstanceState) { |
02 | super .onCreate(savedInstanceState); |
03 |
04 | //设置界面的布局 |
05 | RelativeLayout relativeLayout = new RelativeLayout( this ); |
06 | setContentView(relativeLayout); |
07 |
08 | //添加一个AbsoluteLayout子布局,并给这个布局添加一个button |
09 | AbsoluteLayout abslayout= new AbsoluteLayout ( this ); |
10 | abslayout.setId( 11 ); |
11 | Button btn1 = new Button( this ); |
12 | btn1.setText(” this is a abslayout button”); |
13 | btn1.setId( 1 ); |
14 | AbsoluteLayout.LayoutParams lp0 = new AbsoluteLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, |
15 | ViewGroup.LayoutParams.WRAP_CONTENT, 100 , 0 ); |
16 | abslayout.addView(btn1, lp0 ); |
17 | //将这个子布局添加到主布局中 |
18 | RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); |
19 | lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP); |
20 | lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); |
21 | relativeLayout.addView(abslayout ,lp1); |
22 |
23 | //再添加一个子布局 |
24 | RelativeLayout relativeLayout1 = new RelativeLayout( this ); |
25 | Button btn2 = new Button( this ); |
26 | btn2.setText(” this is a relativeLayout1 button”); |
27 | btn2.setId( 2 ); |
28 | RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); |
29 | lp2.addRule(RelativeLayout.ALIGN_PARENT_TOP); |
30 | lp2.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); |
31 | relativeLayout1.addView(btn2 ,lp2); |
32 |
33 | //将这个布局添加到主布局中 |
34 | RelativeLayout.LayoutParams lp11 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); |
35 | lp11.addRule(RelativeLayout.BELOW , 11 ); |
36 | relativeLayout.addView(relativeLayout1 ,lp11); |
37 | } |