轉自:http://www.itivy.com/android/archive/2011/11/3/android-dialog-customize.html
很多時候,可能Android默認的幾種Dialog對話框已經不能滿足我們的需求,我們需要自定義自己的Dialog對話框,包括樣式的改變,功能的改變等等。今天,我給出一個Android自定義Dialog的例子,大家可以看看,如果這個自定義dialog的方法能夠用得上,那我們完全可以定義出非常富有個性的dialog對話框了,先看一個自定義的dialog對話框效果圖吧,很簡單,只有一個Activity,當點擊Button的時候就彈出這個自定義的Dialog
裏面的幾張圖都比較醜,我不多會美工,隨便用powerpoint畫了幾張圖,原理是一樣的,先不計較這些。下面正入正題
爲了照顧到所有的碼農,在些把所有的代碼都貼出來
新建工程在此就不貼出來了,只是爲了方便大家的複製粘貼,取包名爲com.and.mydialog,主Activity取名爲MyDialogActivity
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
|
package
com.and.mydialog; import
android.app.Activity; import
android.app.AlertDialog; import
android.app.Dialog; import
android.content.DialogInterface; import
android.os.Bundle; import
android.view.KeyEvent; import
android.view.LayoutInflater; import
android.view.Menu; import
android.view.MenuItem; import
android.view.View; import
android.view.View.OnClickListener; import
android.widget.Button; import
android.widget.Toast; public
class
MyDialogActivity extends
Activity { /**
Called when the activity is first created. */ @Override public
void
onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); Button
button = (Button) findViewById(R.id.button1); button.setOnClickListener( new
OnClickListener() { @Override public
void
onClick(View v) { //初始化一個自定義的Dialog Dialog
dialog = new
MyDialog(MyDialogActivity. this , R.style.MyDialog); dialog.show(); } }); } } |
1
2
3
4
5
6
7
8
9
10
|
<? xml
version = "1.0"
encoding = "utf-8" ?> android:orientation = "vertical"
android:layout_width = "fill_parent" android:layout_height = "fill_parent" > < Button android:text = "顯示自定義Dialog" android:id = "@+id/button1" android:layout_height = "wrap_content" android:layout_width = "fill_parent" /> </ LinearLayout > |
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
|
package
com.and.mydialog; import
android.app.Dialog; import
android.content.Context; import
android.os.Bundle; import
android.view.LayoutInflater; import
android.view.View; public
class
MyDialog extends
Dialog { Context
context; public
MyDialog(Context context) { super (context); //
TODO Auto-generated constructor stub this .context
= context; } public
MyDialog(Context context, int
theme){ super (context,
theme); this .context
= context; } @Override protected
void
onCreate(Bundle savedInstanceState) { //
TODO Auto-generated method stub super .onCreate(savedInstanceState); this .setContentView(R.layout.dialog); } } |
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
|
<? xml
version = "1.0"
encoding = "utf-8" ?> android:orientation = "vertical" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:gravity = "center_vertical|center_horizontal" android:background = "@drawable/dialog_bg" > < RelativeLayout android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:paddingLeft = "30dip" android:paddingTop = "10dip" > < ImageView android:id = "@+id/dialog_title_image" android:layout_alignParentLeft = "true" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:background = "@drawable/dialog_title_image" /> < TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_marginLeft = "10dip" android:layout_centerInParent = "true" android:text = "Title" android:layout_toRightOf = "@id/dialog_title_image" android:textColor = "#000000" android:textSize = "30sp" /> </ RelativeLayout > < TextView android:layout_width = "fill_parent" android:layout_height = "1dip" android:background = "@drawable/lins" android:layout_marginTop = "5dip" /> < TextView android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "This
is a custom dialog" android:textColor = "#000000" android:layout_marginTop = "10dip" android:layout_marginLeft = "30dip" /> < RelativeLayout android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:paddingTop = "10dip" android:gravity = "bottom|center_horizontal" android:paddingBottom = "10dip" > < Button android:id = "@+id/dialog_button_cancel" android:layout_alignParentLeft = "true" android:layout_width = "100dip" android:layout_height = "wrap_content" android:text = "確定" /> < Button android:id = "@+id/dialog_button_ok" android:layout_width = "100dip" android:layout_height = "wrap_content" android:layout_toRightOf = "@id/dialog_button_cancel" android:layout_marginLeft = "35dip" android:text = "取消" /> </ RelativeLayout > </ LinearLayout > |
最主要的,是自定義的Style,我們自定義一個式樣,用來改變默認的Dialog樣式
在values文件夾下新建一個styles.xml文件
1
2
3
4
5
6
7
8
9
10
|
<? xml
version = "1.0"
encoding = "utf-8" ?> < resources > < style
name = "MyDialog"
parent = "@android:Theme.Dialog" > < item
name = "android:windowFrame" >@null</ item > < item
name = "android:windowNoTitle" >true</ item >
< item
name = "android:windowBackground" >@drawable/dialog_bg</ item > < item
name = "android:windowIsFloating" >true</ item > < item
name = "android:windowContentOverlay" >@null</ item > </ style > </ resources > |
好了,這樣我們的自定義dialog就基本完成了,雖然是難看了點,但是基本思路就是這樣了,美工好的同學可以用這個思路去寫一個比較漂亮的android自定義dialog對話框。