自定义一个类继承自Dialog类,然后在构造方法中,定义这个dialog的布局和一些初始化信息。
01 |
public class MenuDialog extends Dialog {
|
02 |
03 |
public MenuDialog(Context context, boolean cancelable,
|
04 |
OnCancelListener cancelListener) {
|
05 |
super(context, cancelable, cancelListener);
|
06 |
// TODO Auto-generated constructor stub
|
07 |
}
|
08 |
09 |
public MenuDialog(Context context, int theme) {
|
10 |
super(context, theme);
|
11 |
// TODO Auto-generated constructor stub
|
12 |
}
|
13 |
14 |
public MenuDialog(Context context) {
|
15 |
16 |
//dialog的视图风格 |
17 |
super(context, R.style.Theme_Transparent);
|
18 |
19 |
20 |
//设置布局文件 |
21 |
setContentView(R.layout.menu_dialog);
|
22 |
//setTitle("Custom Dialog");
|
23 |
24 |
//单击dialog之外的地方,可以dismiss掉dialog。 |
25 |
setCanceledOnTouchOutside(true);
|
26 |
27 |
28 |
// 设置window属性
|
29 |
// LayoutParams a = getWindow().getAttributes(); |
30 |
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); |
31 |
// a.gravity = Gravity.TOP; |
32 |
// a.dimAmount = 1.0f; // 添加背景遮盖 |
33 |
// getWindow().setAttributes(a); |
34 |
|
35 |
//在下面这种情况下,后台的activity不会被遮盖,也就是只会遮盖此dialog大小的部分
|
36 |
LayoutParams a = getWindow().getAttributes();
|
37 |
a.gravity = Gravity.TOP;
|
38 |
a.dimAmount = 0.0f; // 去背景遮盖
|
39 |
getWindow().setAttributes(a);
|
40 |
41 |
42 |
43 |
44 |
//为你的对话框初始化数据 |
45 |
initMenu();
|
46 |
}
|
47 |
48 |
} |
然后再需要此dialog的地方,实例化这个dialog就行了。
另附此对话框的主题:
01 |
<style name="Theme.Transparent" parent="android:Theme">
|
02 |
03 |
<item name="android:windowBackground">@drawable/dialog_box_2</item>//此对话框的背景
|
04 |
<item name="android:windowIsTranslucent">true</item>//对话框是否透明
|
05 |
<item name="android:windowContentOverlay">@null</item>//对话框是否有遮盖
|
06 |
<item name="android:windowNoTitle">true</item>//对话框无标题
|
07 |
<item name="android:windowIsFloating">true</item>//对话框是否浮动
|
08 |
<item name="android:backgroundDimEnabled">false</item>
|
09 |
10 |
</style>
|
文章出处:http://blog.csdn.net/ooo4561213/article/details/6655748