Dialog (ix)--use WindowManager to add Dialog anywhere on the screen

Source: Internet
Author: User

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>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.