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> |
以上就是本文的全部內容,希望對大家的學習有所幫助。