Android Popupwindow Learning Summary

Source: Internet
Author: User

There is a pop-up window that can be placed anywhere in Android Popupwindow, more flexible and convenient than Dialog , but the author has encountered several problems in the use of the process, to summarize today.

First of all, how to put a Popupwindow,popupwindow a total of three methods are available:

Showasdropdown (View anchor): relative to the position of a control (just below the left), no offset

Showasdropdown (View anchor, int xoff, int yoff): Offset relative to the position of a control

Showatlocation (View parent, int gravity, int x, int y): can be set offset or no offset relative to the parent control's position (for example, central Gravity.center, Lower gravity.bottom, etc.)

These methods of the difference in detail everyone from the literal meaning can also be understood, this side is not detailed, not clear can test their own.


The following is mainly about the author himself in the use of some of the problems encountered in the process

1. Popupwindow Creation Method

The first method of creation:

Layoutinflater Inflater = (layoutinflater) this.getsystemservice (context.layout_inflater_service);//Introduction Window profile view View = Inflater.inflate (R.layout.pop_view, NULL);//Create Popupwindow object/*** Create mode one ****/pop = new Popupwindow (view, Layoutparams.wrap_content, layoutparams.wrap_content);//settings Click the window outside the window disappears pop.setoutsidetouchable (TRUE);//Set this parameter to get the focus, Otherwise, you cannot click Pop.setfocusable (TRUE);

The second method of creation:

Layoutinflater Inflater = (layoutinflater) this.getsystemservice (context.layout_inflater_service);//Introduction Window profile view View = Inflater.inflate (R.layout.pop_view, NULL);//Create Popupwindow object/*** Create mode two ****/pop = new Popupwindow (this); Pop.setcontentview (view);p op.setwidth (layoutparams.wrap_content);p op.setheight (layoutparams.wrap_content);// Setting the window outside the window disappears pop.setoutsidetouchable (TRUE);//Set this parameter to get the focus, otherwise you cannot click Pop.setfocusable (TRUE);

What is the difference between the two ways, this difference I also found in a very accidental situation, if you do not add this code in the code:

Pop.setbackgrounddrawable (New bitmapdrawable ());

Then there is a clear difference between the two ways:as the first method is created, clicking on another area Popupwindow does not disappear, and is created in the second way, the Popupwindow will appear with a gray border

That pop.setbackgrounddrawable (new bitmapdrawable ()); What is the function of that code, which is to set a background for Popupwindow, because the second way to create Popupwindow,Popupwindow will default to add a gray background box, if you want to remove the background box, you can use this code, but to this side there is a second problem:

2. setbackgrounddrawable () issue

Some people may think that since the background box is removed, it can be done with setbackgrounddrawable (null), which is the author's first practice, but in the second way to create Popupwindow, if the use of Setbackgrounddrawable (NULL) This kind of writing, will appear to click the other area Popupwindow does not disappear the question,Oh,mygod, this is why, this is also the author has been puzzled of place, Domestic and foreign turn a circle also did not see someone answer this question reason, only said the solution, is to use setbackgrounddrawable (New bitmapdrawable ()) Such writing can avoid this problem , OK, no way, I can only know it, and I don't know why.

3. Get Popupwindow width and height problem

the author did not set a fixed width for Popupwindow when creating the Popupwindow, so the problem arose when getting the Popupwindow wide, and I used layoutparams.wrap_content to set the width of Popupwindow, the intention is to make the popupwindow adaptive size, but when I use the following two lines of code to get Popupwindow when the width of the high, the value is -2,why?

System.out.println ("Pop width =" +pop.getwidth ()); System.out.println ("Pop height =" +pop.getheight ());

Then I looked around and found out that the return wasthe value of Layoutparams.wrap_content-2, instead of returning the width and height of the Popupwindow display, looks like this Popupwindow is what value you give him when you set it, and he returns what value, Although you set the layoutparams.wrap_content, but when you get the Popupwindow not return the final value of his display, but just return the value you give him, so I think, I first will show the size of the layout of the figure, Then set to Popupwindow, that gets the correct value, not much to say, on the code:

If you want to get the value of the actual layout size of the Popupwindow object, you can use the following code: View.measure (Layoutparams.wrap_content, layoutparams.wrap_content); int Viewwidth = View.getmeasuredwidth (), int viewheight = View.getmeasuredheight ();p op.setwidth (viewwidth);p op.setheight (Viewheight);

The problem is almost the same, these seemingly small problems, but at that time, it took the author a lot of time to troubleshoot and solve, and then give me all the test code:

Activity_main.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:o rientation= "vertical" >    <button        android:id= "@+id/btn"        android:layout_width= "Wrap_content"        android:layout_height= "wrap_content"        android:text= "Dianji"/></linearlayout>

Mainactivity

Package Com.example.popupwindowdemo;import Android.app.activity;import Android.content.context;import Android.graphics.drawable.bitmapdrawable;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup.layoutparams;import Android.widget.button;import Android.widget.popupwindow;public class Mainactivity extends Activity {/** Called when th E activity is first created.        */private Popupwindow pop;        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);//Layoutinflater Inflater = Layoutinflater.from (this);        Layoutinflater Inflater = (layoutinflater) this.getsystemservice (Context.layout_inflater_service);        Introduction Window profile View view = Inflater.inflate (R.layout.pop_view, NULL); Create Popupwindow object/*** Create pattern one ****///pop = new Popupwindow(View, layoutparams.wrap_content, layoutparams.wrap_content);        /*** Create mode two ****/pop = new Popupwindow (this); Pop.setcontentview (view),//////If the Popupwindow object is set to a width or height of layoutparams.wrap_content, then getwidth () Return is the value of layoutparams.wrap_content, i.e. -2//pop.setwidth (layoutparams.wrap_content);//Pop.setheight (LayoutParams . wrap_content)/////If you want to get the value of the actual layout size of the Popupwindow object, you can use the following code: View.measure (Layoutparams.wrap_content, layou        Tparams.wrap_content);        int viewwidth = View.getmeasuredwidth ();                int viewheight = View.getmeasuredheight ();        Pop.setwidth (Viewwidth);                Pop.setheight (Viewheight);        System.out.println ("Pop width =" +pop.getwidth ());                System.out.println ("Pop height =" +pop.getheight ()); This parameter must be set, if not set, in the mode, then the click Other area Popupwindow will not disappear, in mode two, Popupwindow will appear gray border pop.setbackgrounddrawable (new        Bitmapdrawable ());//pop.setbackgrounddrawable (NULL);    Set Click Window outside window disappears    Pop.setoutsidetouchable (TRUE);                Set this parameter to get focus, otherwise you cannot click Pop.setfocusable (TRUE);        Button btn = (button) Findviewbyid (R.ID.BTN);                Btn.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {                if (pop.isshowing ()) {//Hide window, if set the click Window outside the hour that does not need this way to hide Pop.dismiss ();                } else {//display window Pop.showasdropdown (v);    }                            }        }); }}




Android Popupwindow Learning Summary

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.