Android Customization dialog box

Source: Internet
Author: User

Android Customization dialog box

dialog box commonly used have Popupwindow and dialog generally need to rewrite layout file examples are as follows

Code

 PackageCom.example.popup_vs_dialog;Importandroid.app.Activity;ImportAndroid.app.Dialog;ImportAndroid.graphics.Rect;Importandroid.graphics.drawable.ColorDrawable;ImportAndroid.os.Bundle;Importandroid.view.Gravity;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.view.ViewGroup.LayoutParams;ImportAndroid.view.Window;ImportAndroid.view.WindowManager;ImportAndroid.widget.ImageView;ImportAndroid.widget.PopupWindow; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); FinalImageView img1 = (ImageView) This. Findviewbyid (R.ID.IMG1); Img1.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {usepopup (IMG1);                }        }); FinalImageView Img2 = (ImageView) This. Findviewbyid (R.ID.IMG2); Img2.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {usedialog (IMG2);    }        }); }        Private voidUsepopup (FinalImageView anchor) {Layoutinflater Minflater= Layoutinflater.from ( This); ViewGroup Rootview= (ViewGroup) minflater.inflate (R.layout.menu,NULL); Rootview.setlayoutparams (Newlayoutparams (layoutparams.wrap_content, layoutparams.wrap_content)); FinalPopupwindow popup =NewPopupwindow ( This); //Be sure to set the width height before setcontentview, otherwise do not displaypopup.setwidth (WindowManager.LayoutParams.WRAP_CONTENT);        Popup.setheight (WindowManager.LayoutParams.WRAP_CONTENT); //Remove the default backgroundPopup.setbackgrounddrawable (Newcolordrawable (Android.        R.color.transparent));        Popup.setcontentview (Rootview); //Popupwindow disappears when you click on the empty space.Popup.settouchable (true); Popup.setoutsidetouchable (true); //if focusable is false, an activity pops up with a popupwindow, pressing the back key, and as Popupwindow has no focus, it exits the activity directly. If focusable is ejected for True,popupwindow, all touch screens and physical keys are popupwindows processed. Popup.setfocusable (true); //calculate the position of a bullet box        int[] xy =Calcpopupxy (Rootview,anchor); //use Absolute (x, y) coordinates without any gravityPopup.showatlocation (View) anchor.getparent (), gravity.no_gravity, xy[0], xy[1]); }        Private voidUsedialog (FinalImageView anchor) {Layoutinflater Minflater= Layoutinflater.from ( This); ViewGroup Rootview= (ViewGroup) minflater.inflate (R.layout.menu,NULL); Rootview.setlayoutparams (Newlayoutparams (layoutparams.wrap_content, layoutparams.wrap_content)); Dialog Dialog=NewDialog ( This); Windowmanager.layoutparams params=Dialog.getwindow (). GetAttributes (); Params.width=WindowManager.LayoutParams.WRAP_CONTENT; Params.height=WindowManager.LayoutParams.WRAP_CONTENT; //Remove the default background, the following two can beDialog.getwindow (). Setbackgrounddrawable (Newcolordrawable (Android.        R.color.transparent)); //Dialog.getwindow (). Setbackgrounddrawableresource (Android.        R.color.transparent); //Http://stackoverflow.com/questions/12348405/dialog-is-bigger-than-expected-when-using-relativelayout        //dialog By default, there is a titleDialog.requestwindowfeature (Window.feature_no_title);//Remove the title, otherwise it will affect the height calculation, must be called before Setcontentview, finally understand that there is a set of theme the purpose of the constructorDialog.setcontentview (Rootview); //calculate the position of a bullet box        int[] xy =Calcpopupxy (Rootview,anchor); //the default value for Gravity is Gravity.center, which is Gravity.center_horizontal |        Gravity.center_vertical. //Reference:http://www.cnblogs.com/angeldevil/archive/2012/03/31/2426242.htmlDialog.getwindow (). Setgravity (Gravity.left |gravity.top); Params.x= Xy[0]; Params.y= Xy[1];    Dialog.show (); }        Private int[] calcpopupxy (View Rootview, view anchor) {intw = View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED); inth = View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED);          Rootview.measure (W, h); intPopupwidth =rootview.getmeasuredwidth (); intPopupheight =rootview.getmeasuredheight (); Rect Anchorrect=Getviewabsolutelocation (anchor); intx = Anchorrect.left + (anchorrect.right-anchorrect.left)/2-POPUPWIDTH/2; inty = anchorrect.top-Popupheight; return New int[]{x,y}; }         Public StaticRect getviewabsolutelocation (view view) {if(View = =NULL){            return NewRect (); }        //gets the coordinates of the view relative to the screen        int[] location =New int[2] ; View.getlocationonscreen (location);//This is to get the absolute coordinates relative to the screen, and View.getlocationinwindow (location); is to get the relative coordinates on the window, in this case there is only one window, which is equivalent//get the view's wide height        intwidth =view.getmeasuredwidth (); intHeight =view.getmeasuredheight (); //gets the rect of the viewRect rect =NewRect (); Rect.left= Location[0]; Rect.top= Location[1]; Rect.right= Rect.left +width; Rect.bottom= Rect.top +height; returnrect; }}

Xml

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <ImageViewAndroid:id= "@+id/img1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_centerinparent= "true"android:src= "@drawable/img" />        <ImageViewAndroid:id= "@+id/img2"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_torightof= "@id/img1"Android:layout_marginleft= "50DP"Android:layout_aligntop= "@id/img1"android:src= "@drawable/img" /></Relativelayout>

Android Customization dialog box

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.