自定义Dialog步骤

1.在values中的style中设置好你将要弹出的Dialog的样式,例如背景,颜色,边框等

<style name="dialogAnimation" parent="android:Animation">
        <item name="android:windowEnterAnimation">@android:anim/slide_in_left</item>
        <item name="android:windowExitAnimation">@android:anim/slide_out_right</item>
    </style>
    
    <style name="my_dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item><!-- 窗口边框 -->
        <item name="android:windowIsFloating">true</item><!-- 窗口是否浮动在Activity上 -->
        <item name="android:windowIsTranslucent">true</item><!-- 半透明 -->
        <item name="android:windowNoTitle">true</item><!-- 没有标题栏 -->
        <item name="android:background">@null</item>
        <item name="android:windowBackground">@drawable/dialog_bg</item>
        <item name="android:backgroundDimEnabled">true</item><!-- 模糊 -->
    </style>


2.在Activity中设置对其进行设置

private void showCustomDialog() {
		if (d == null) {
			d = new Dialog(this, R.style.my_dialog);
			// 设置进出动画
			d.getWindow().setWindowAnimations(R.style.dialogAnimation);
			d.setOnDismissListener(new OnDismissListener() {

				@Override
				public void onDismiss(DialogInterface dialog) {
					// TODO Auto-generated method stub

				}
			});
			d.setOnCancelListener(new OnCancelListener() {
				
				@Override
				public void onCancel(DialogInterface dialog) {
					// TODO Auto-generated method stub
					
				}
			});
			//按返回键时可以隐藏dialog
			d.setCancelable(true);
			// 显示弹出框
			d.show();
			View layout = getLayoutInflater().inflate(R.layout.dialog_layout,
					null);
			// 设置布局
			d.getWindow().setContentView(layout);
			layout.findViewById(R.id.sure).setOnClickListener(this);
			layout.findViewById(R.id.cancel).setOnClickListener(this);
                        WindowManager.LayoutParams lp = enterDialog.getWindow()
						.getAttributes();
		        lp.width = (int) (LApplication.metrics.widthPixels * 2 / 3); // 设置宽度
			enterDialog.getWindow().setAttributes(lp);
		} else {
			d.show();
		}
	}


你可能感兴趣的:(自定义Dialog步骤)