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 |
package
com.example.alertdialog; import
android.os.Bundle; import
android.view.LayoutInflater; import
android.view.View; import
android.view.View.OnClickListener; import
android.view.Window; import
android.view.WindowManager; import
android.widget.Button; import
android.widget.TextView; import
android.app.Activity; import
android.app.AlertDialog; public
class
MainActivity extends
Activity { @Override protected
void
onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); final
AlertDialog dialog= new
AlertDialog.Builder( this ).create(); View dialogview=LayoutInflater.from( this ).inflate(R.layout.newdialogxml, null ); dialog.setView(dialogview); dialog.setTitle( "自定义Dialog1" ); ((Button)(findViewById(R.id.action_settings))).setOnClickListener( new
OnClickListener() { @Override public
void
onClick(View v) { // TODO Auto-generated method stub dialog.show(); } }); ((Button)(dialogview.findViewById(R.id.button))).setOnClickListener( new
OnClickListener() { @Override public
void
onClick(View v) { // TODO Auto-generated method stub dialog.cancel(); } }); ((Button)(findViewById(R.id.action_settings2))).setOnClickListener( new
OnClickListener() { @Override public
void
onClick(View v) { // TODO Auto-generated method stub ShowDialogtwo(); } private
void
ShowDialogtwo() { // TODO Auto-generated method stub final
AlertDialog dialog= new
AlertDialog.Builder(MainActivity. this ).create(); dialog.show(); Window window=dialog.getWindow(); /* * getwindow后背景会变成灰色,此3行代码给背景相当于给了给透明色 * */ WindowManager.LayoutParams params = window.getAttributes(); params.dimAmount = 0f; window.setAttributes(params); window.setContentView(R.layout.newdialogxml); window.setTitle( "自定义Dialog2" ); ((Button)(window.findViewById(R.id.button))).setOnClickListener( new
OnClickListener() { @Override public
void
onClick(View v) { // TODO Auto-generated method stub dialog.cancel(); } }); } }); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 |
<?xml version= "1.0"
encoding= "utf-8" ?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:orientation= "vertical"
> <TextView android:gravity= "center" android:layout_gravity= "center_vertical" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "这是一个自定义dialog" /> <Button android:id= "@+id/button" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "取消" /> </LinearLayout> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 |
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <Button android:id= "@+id/action_settings" android:gravity= "center" android:layout_gravity= "center" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "查看自定义Dialog1"
/> <Button android:id= "@+id/action_settings2" android:gravity= "center" android:layout_gravity= "center" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "查看自定义Dialog2"
/> </LinearLayout> |
原文:http://www.cnblogs.com/androidxufeng/p/3615747.html