Dialog(五)——自定义对话框(Dialog)

MainActivity如下:

package cn.testalertdialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
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;
/**
 * Demo说明:
 * 
 * 自定义Dialog的使用
 * 
 * 1 在屏幕中显示自定义Dialog.
 *   其中按照屏幕大小设置Dialog的显示大小
 *   
 * 2 在屏幕底部显示自定义Dialog.
 *   网上有许多资料都有一个缺陷:Dialog距离屏幕底部总有几毫米的间隙.
 *   试过许多方法和style都不管用.在此给出一个可行的办法.详见代码.
 * 3 本示例采用API 15即 4.0.3
 * 
 * http://blog.csdn.net/lfdfhl
 * 
 */
public class MainActivity extends Activity {
    private Button mButton;
    private Dialog mDialog;
    private AlertDialog mAlertDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}
    private void init(){
    	mButton=(Button) findViewById(R.id.button);
    	mButton.setOnClickListener(new ButtonOnClickListenerImpl());
    }

	private class ButtonOnClickListenerImpl implements OnClickListener {
		@Override
		public void onClick(View view) {
			switch (view.getId()) {
			case R.id.button:
				showDialog(MainActivity.this);
				//showDialogAtScreenBottom(MainActivity.this);
				break;
			default:
				break;
			}
		}

	}
	
	//显示自定义Dialog
	private void showDialog(Context context) {
		LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		View dialogView = inflater.inflate(R.layout.dialogview, null);
		mDialog = new Dialog(context, R.style.dialog);
		mDialog.setContentView(dialogView);
		Window dialogWindow = mDialog.getWindow();
		// 获取对话框当前的参数值
		WindowManager.LayoutParams layoutParams = dialogWindow.getAttributes();
		// 设置对话框高度为屏幕高度的0.3倍
		layoutParams.height = (int) (getScreenHeight(context) * 0.3);
		// 设置对话框宽度为屏幕宽度的0.7倍
		layoutParams.width = (int) (getScreenWidth(context) * 0.7);
		dialogWindow.setAttributes(layoutParams);
		mDialog.show();
		initDialog(dialogView);
	}
	
	//在屏幕底部显示自定义Dialog
	private void showDialogAtScreenBottom(Context context) {
		LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		View dialogView = inflater.inflate(R.layout.dialogview, null);
		mAlertDialog = new AlertDialog.Builder(context, R.style.dialog).create();
		mAlertDialog.show();
		Window window = mAlertDialog.getWindow();
		// 将Dialog显示到屏幕的底部
		window.setGravity(Gravity.BOTTOM);
		// 去掉Dialog本身的padding
		window.getDecorView().setPadding(0, 0, 0, 0);
		WindowManager.LayoutParams layoutParams = mAlertDialog.getWindow().getAttributes();
		// 设置宽度为屏幕宽度
		layoutParams.width = getScreenWidth(context);
		mAlertDialog.getWindow().setAttributes(layoutParams);
		mAlertDialog.setContentView(dialogView);
		initAlertDialog(dialogView);
	}
	
	
	//初始化自定义布局中的控件
	private void initDialog(View dialogView){
		  Button firstButton=(Button) dialogView.findViewById(R.id.dialog_firstButton);
		  firstButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					System.out.println("点击确定");
				}
			});
		  
		  Button secondButton=(Button) dialogView.findViewById(R.id.dialog_secondButton);
		  secondButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (mDialog!=null&&mDialog.isShowing()) {
						mDialog.dismiss();
					}
				}
			});
	}
	
	//初始化自定义布局中的控件
	private void initAlertDialog(View dialogView) {
		Button firstButton = (Button) dialogView.findViewById(R.id.dialog_firstButton);
		firstButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				System.out.println("点击确定");
			}
		});

		Button secondButton = (Button) dialogView.findViewById(R.id.dialog_secondButton);
		secondButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mAlertDialog != null && mAlertDialog.isShowing()) {
					mAlertDialog.dismiss();
				}
			}
		});
	}
	
	/**
	 * 获得屏幕高度
	 */
	public static int getScreenWidth(Context context) {
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(outMetrics);
		return outMetrics.widthPixels;
	}

	/**
	 * 获得屏幕宽度
	 */
	public static int getScreenHeight(Context context) {
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(outMetrics);
		return outMetrics.heightPixels;
	}

}



main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_tip" 
        android:textSize="20sp"
        android:layout_centerInParent="true"
     />

</RelativeLayout>

dialogview.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/white"
    android:padding="50px"
    android:orientation="vertical" >

    <Button
        android:id="@+id/dialog_firstButton"
        android:layout_width="200dip"
        android:layout_height="50dip"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="50px"
        android:background="@android:color/holo_blue_dark"
        android:text=" 确  定  " />

    <Button
        android:id="@+id/dialog_secondButton"
        android:layout_width="200dip"
        android:layout_height="50dip"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="50px"
        android:background="@android:color/holo_blue_dark"
        android:text=" 取  消  " />

</LinearLayout>


style如下:

<style name="dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>


你可能感兴趣的:(自定义,dialog,对话框,AlertDialog)