xml对开发者来说十分的方便,不仅使用起来简单,而且能够及时调试,修改界面之后马上能看到效果。
java设置布局不具有这个优势。但是java却可以动态对布局进行操作,这是xml所做不到的。笔者认为,新手索要掌握的java动态设置布局主要有两点,一方面是对布局的属性进行修改,另一方面是增添和删除控件。
首先说一下动态设置布局在项目中的应用,拿高德地图举个例子,如下图:
我们可以看到,高德地图的默认界面与点击地图之后的界面是不一样的,上面同样的控件在layout中的位置也不一样,这个用xml便是难以实现的了,于是java动态设置布局便有了其重要性。
接下来看一下分享的demo效果:
代码其实比较容易理解,具体的解释已经注释在代码中了,读者可以自己写了理解一下。
mainactivity:
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
package com.example.activeuitest; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.button; import android.widget.linearlayout; import android.widget.radiogroup; import android.widget.relativelayout; public class mainactivity extends appcompatactivity implements view.onclicklistener{ private button bt_gone; //让布局隐藏 private button bt_visiable; //让布局显示 private button bt_add; //增添布局 private button bt_delete; //删除布局 private relativelayout rl_main; private radiogroup rl_radiogroup; private relativelayout rl_infotip; private linearlayout ll_test; @override protected void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.activity_main); init(); //初始化 } private void init() { bt_gone= (button) findviewbyid(r.id.button1); bt_visiable= (button) findviewbyid(r.id.button2); bt_add= (button) findviewbyid(r.id.button3); bt_delete= (button) findviewbyid(r.id.button4); rl_main=(relativelayout)findviewbyid(r.id.main_layout); rl_radiogroup=(radiogroup)findviewbyid(r.id.radio_group); rl_infotip=(relativelayout)findviewbyid(r.id.info_tip); //此处要获取其他xml的控件需要先引入改layout的view(这个linearlayout用于演示添加和删除) view view= layoutinflater.from( this ).inflate(r.layout.test_linear_layout, null , false ); ll_test=(linearlayout)view.findviewbyid(r.id.test_layout); bt_gone.setonclicklistener( this ); bt_visiable.setonclicklistener( this ); bt_add.setonclicklistener( this ); bt_delete.setonclicklistener( this ); } @override public void onclick(view v) { switch (v.getid()){ case r.id.button1: rl_infotip.setvisibility(view.gone); //底部tip设置不可见 //初始化宽高属性 relativelayout.layoutparams lp1 = new relativelayout.layoutparams( viewgroup.layoutparams.wrap_content, viewgroup.layoutparams.wrap_content); lp1.addrule(relativelayout.align_parent_bottom); //设置置底 lp1.setmargins( 10 , 0 , 0 , 10 ); //设置margin,此处单位为px rl_radiogroup.setlayoutparams(lp1); //动态改变布局 break ; case r.id.button2: rl_infotip.setvisibility(view.visible); //底部tip设置可见 //初始化宽高属性 relativelayout.layoutparams lp2 = new relativelayout.layoutparams( viewgroup.layoutparams.wrap_content, viewgroup.layoutparams.wrap_content); lp2.setmargins( 10 , 0 , 0 , 10 ); //设置margin,此处单位为px lp2.addrule(relativelayout.above, r.id.info_tip); //设置above,让控件于r.id.info_tip之上 rl_radiogroup.setlayoutparams(lp2); //动态改变布局 break ; case r.id.button3: //初始化宽高属性,此处单位为px relativelayout.layoutparams lp3 = new relativelayout.layoutparams( 200 , 200 ); lp3.addrule(relativelayout.below, r.id.button4); //设置below,让控件于r.id.button4之下 rl_main.addview(ll_test, lp3); //动态改变布局 ll_test.setvisibility(view.visible); //此处需要设置布局显示,否则会不显示 break ; case r.id.button4: rl_main.removeview(ll_test); //动态改变布局 break ; } } } |
activity_main:
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
<?xml version= "1.0" encoding= "utf-8" ?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:id= "@+id/main_layout" > <button android:id= "@+id/button1" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "隐藏" /> <button android:id= "@+id/button2" android:layout_below= "@+id/button1" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "显示" /> <button android:id= "@+id/button3" android:layout_below= "@+id/button2" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "添加布局" /> <button android:id= "@+id/button4" android:layout_below= "@+id/button3" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "删除布局" /> <radiogroup android:id= "@+id/radio_group" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:padding= "5dp" android:layout_marginleft= "10px" android:layout_marginbottom= "10px" android:orientation= "horizontal" android:layout_above= "@+id/info_tip" android:background= "@android:color/darker_gray" > <textview android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "精确度:" /> <radiobutton android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:checked= "true" android:text= "普通" android:textcolor= "@android:color/black" /> <radiobutton android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "精准" android:textcolor= "@android:color/black" /> </radiogroup> <relativelayout android:id= "@+id/info_tip" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:layout_alignparentbottom= "true" android:paddingleft= "10dp" android:paddingright= "10dp" android:paddingtop= "20dp" android:background= "@android:color/darker_gray" > <textview android:id= "@+id/info_tip_name" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "受灾地点" android:textcolor= "@android:color/black" android:textsize= "20dp" /> <textview android:id= "@+id/info_tip_distance" android:layout_below= "@+id/info_tip_name" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "受灾距离" /> <textview android:id= "@+id/info_tip_address" android:layout_torightof= "@+id/info_tip_distance" android:layout_below= "@+id/info_tip_name" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_marginleft= "10dp" android:text= "受灾地址" /> <button android:layout_alignparentright= "true" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "详情" /> <linearlayout android:layout_below= "@+id/info_tip_address" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:layout_margintop= "10dp" android:orientation= "horizontal" > <button android:layout_width= "0dp" android:layout_weight= "1" android:layout_height= "wrap_content" android:text= "驾车" /> <button android:layout_width= "0dp" android:layout_weight= "1" android:layout_height= "wrap_content" android:text= "公交" /> <button android:layout_width= "0dp" android:layout_weight= "1" android:layout_height= "wrap_content" android:text= "步行" /> </linearlayout> </relativelayout> </relativelayout> |
test_linear_layout:
1
2
3
4
5
6
7
8
9
10
11
|
<?xml version= "1.0" encoding= "utf-8" ?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "200dp" android:layout_height= "200dp" android:background= "@android:color/holo_blue_bright" android:id= "@+id/test_layout" android:orientation= "horizontal" > </linearlayout> |
以上就是本文的全部内容,希望对大家的学习有所帮助。