关于 android 常用布局,利用 xml 文件实现已经有很多的实例了。但如何利用代码实现呢?当然利用代码实现没有太大的必要,也是不提倡的,但我觉得利用代码实现这些布局,可以更好的了解 sdk api ,所以在此也整理一些,和大家分享一下。
首先看一下,布局所对应的类的 api 继承图:
android常用布局的代码实现所有的布局都会对应相关的类,这些类都是继承自 android.view.viewgroup 类的。而 linearlayout,relativelayout 都是在 android.widget 包里的。另外,tablelayout 是继承自 linearlayout.
下面直接贴代码了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
// 利用代码设置 线性布局 private void setlinearlayout(){ linearlayout llayout = new linearlayout( this ); llayout.setorientation(linearlayout.vertical); // 设置线性布局的排列方式 textview textview = new textview( this ); textview.settext( "代码实现的线性布局" ); textview.settextcolor(color.red); textview.setgravity(gravity.center); // 设置文本内容的对齐方式 linearlayout.layoutparams ll_lpara = new linearlayout.layoutparams(mp,wc); // ll_lpara.gravity = gravity.center_horizontal; // 设置控件在布局中的对齐方式 llayout.addview(textview,ll_lpara); button btn = new button( this ); btn.settext( "按钮" ); llayout.addview(btn,ll_lpara); // 按指定属性添加控件 setcontentview(llayout); } |
实现效果图:
=========================================================================
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
// 利用代码设置 相对布局 private void setrelativelayout(){ relativelayout rlayout = new relativelayout( this ); rlayout.setpadding( 10 , 10 , 10 , 10 ); // 单位: pixels int textviewid = 100 ; textview textview = new textview( this ); textview.setid(textviewid); textview.settext( "请输入:" ); relativelayout.layoutparams rl_lpara1 = new relativelayout.layoutparams(mp, wc); rlayout.addview(textview, rl_lpara1); int edittextid = 200 ; edittext edittext = new edittext( this ); edittext.setid(edittextid); edittext.setbackgroundresource(android.r.drawable.editbox_background); // 设置背景 , 同android:backgroumd relativelayout.layoutparams rl_lpara2 = new relativelayout.layoutparams(mp, wc); rl_lpara2.addrule(relativelayout.below,textviewid); // 设置相对属性,需先指定相对控件的id rlayout.addview(edittext, rl_lpara2); int backbtnid = 300 ; button backbtn = new button( this ); backbtn.setid(backbtnid); backbtn.settext( "返回" ); relativelayout.layoutparams rl_lpara3 = new relativelayout.layoutparams(wc, wc); rl_lpara3.addrule(relativelayout.below, edittextid); rl_lpara3.addrule(relativelayout.align_parent_right); // 设置与父控件的相对属性 rlayout.addview(backbtn, rl_lpara3); button okbtn = new button( this ); okbtn.settext( "确定" ); relativelayout.layoutparams rl_lpara4 = new relativelayout.layoutparams(wc, wc); rl_lpara4.addrule(relativelayout.left_of, backbtnid); rl_lpara4.addrule(relativelayout.align_top,backbtnid); rlayout.addview(okbtn, rl_lpara4); setcontentview(rlayout); } |
实现效果图:
=========================================================================
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
// 利用代码设置 表格布局 private void settablelayout(){ tablelayout tlayout = new tablelayout( this ); tlayout.setcolumnstretchable( 2 , true ); // 拉长索引从0开始的第2列 tablelayout.layoutparams tl_lpara = new tablelayout.layoutparams(mp,wc); // 1. tablerow 不需要设置 layout_width, layout_height // 2. tablerow 中的控件不能设置 layout_span 属性 tablerow tr1 = new tablerow( this ); textview textview0 = new textview( this ); textview0.settext( "第0列" ); tr1.addview(textview0); textview textview1 = new textview( this ); textview1.settext( "第1列" ); tr1.addview(textview1); textview textview2 = new textview( this ); textview2.settext( "第2列" ); textview2.setbackgroundcolor(color.cyan); tr1.addview(textview2); tlayout.addview(tr1, tl_lpara); tablerow tr2 = new tablerow( this ); button btn0 = new button( this ); btn0.settext( "按钮0" ); tr2.addview(btn0); button btn1 = new button( this ); btn1.settext( "按钮1" ); tr2.addview(btn1); button btn2 = new button( this ); btn2.settext( "按钮2" ); tr2.addview(btn2); button btn3 = new button( this ); btn3.settext( "按钮3" ); tr2.addview(btn3); tlayout.addview(tr2, tl_lpara); setcontentview(tlayout); } |
实现效果图: