Mainactivity as follows:
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 Description: * Use WindowManager to add and remove custom dialogs from anywhere on the screen * essentially using the WindowManager to add a view to the screen, just * We made this view a common dialog style * * Reference: * http://blog.csdn.net/lfdfhl/article/details/8195466 * */public class mainactivity Exte NDS Activity {private View mdialogview; Private WindowManager Mwindowmanager; @Overrideprotected 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 ();p arams.height = WindowManager.LayoutParams.WRAP _content;Params.width = Windowmanager.layoutparams.wrap_content;params.y + = 100;params.x + = -30;initdialogcomponents ( Mdialogview);//Add dialog box Mwindowmanager.addview (Mdialogview, params);} Initialize the control in a custom dialog box private void Initdialogcomponents (View dialogview) {button firstbutton= (button) Dialogview.findviewbyid (R.id.dialog_firstbutton); Firstbutton.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {System.out.println (" Click on the first button ");}); Button secondbutton= (button) Dialogview.findviewbyid (R.id.dialog_secondbutton); Secondbutton.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {if (mdialogview!=null) {/ /Remove dialog box Mwindowmanager.removeview (Mdialogview);}});}}
Main.xml as follows:
<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= "use WindowManager to add custom dialogs anywhere on the screen" android:textsize= "20SP" Android:layout_centerhorizontal= "true" /></relativelayout>
Dialogview.xml as follows:
<?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:o rientation= "vertical" > <button android:id= "@+id/dialog_firstbutton" android:layout_width= " 150dip " android:layout_height=" 50dip " android:text=" click button " android:layout_gravity=" Center_ Horizontal " /> <button android:id=" @+id/dialog_secondbutton " android:layout_width=" 150dip " android:layout_height=" 50dip " android:text=" Remove dialog box " android:layout_gravity=" Center_ Horizontal " /></linearlayout>