首页 > 代码库 > Dialog(九)——利用WindowManager在屏幕任意位置添加Dialog

Dialog(九)——利用WindowManager在屏幕任意位置添加Dialog

MainActivity如下:
package cn.testalertdialog;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
/**
 * Demo描述:
 * 利用WindowManager在屏幕任意位置添加和移除"自定义对话框"
 * 本质上是使用了WindowManager在屏幕上添加了View,只是
 * 我们把这个View做成了常见的对话框的样式
 * 
 * 参考资料:
 * http://blog.csdn.net/lfdfhl/article/details/8195466
 * 
 */
public class MainActivity extends Activity {
    private View mDialogView;
    private WindowManager mWindowManager;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}

	private void init() {
		mWindowManager = getWindowManager();
		LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		mDialogView = inflater.inflate(R.layout.dialogview, null);
		WindowManager.LayoutParams params = new WindowManager.LayoutParams();
		params.height = WindowManager.LayoutParams.WRAP_CONTENT;
		params.width = WindowManager.LayoutParams.WRAP_CONTENT;
		params.y += 100;
		params.x += -30;
		initDialogComponents(mDialogView);
		//添加对话框
		mWindowManager.addView(mDialogView, params);
	}

	
	//初始化自定义对话框中的控件
	private void initDialogComponents(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");
				}
			});
		  
		  Button secondButton=(Button) dialogView.findViewById(R.id.dialog_secondButton);
		  secondButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (mDialogView!=null) {
						//移除对话框
						mWindowManager.removeView(mDialogView);
					}
				}
			});
	}

}

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="利用WindowManager在屏幕任意位置添加自定义对话" 
        android:textSize="20sp"
        android:layout_centerHorizontal="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:orientation="vertical" >
    <Button 
        android:id="@+id/dialog_firstButton"
        android:layout_width="150dip"
        android:layout_height="50dip"
        android:text="点击Button"
        android:layout_gravity="center_horizontal"
        />
    
     <Button 
        android:id="@+id/dialog_secondButton"
        android:layout_width="150dip"
        android:layout_height="50dip"
        android:text="移除对话框"
        android:layout_gravity="center_horizontal"
        />

</LinearLayout>