Poprpwindow
In the Android pop-up box I currently learned that there are three kinds: alertdialog,poprpwindow,activity pseudo-bullet box,
Alertdialog is so familiar, it's not introduced here.
Let's see Poprpwindow.
The explanations given by the API are:
It means a pop-up form showing the view that will float on top of the current activity,
The difference between it and alertdialog is that there are two ways to pop up a box in Android: Alertdialog and Popupwindow, which are different:
1, the position of Alertdialog fixed, and the position of Popupwindow can be arbitrary;
2. Alertdialog is non-blocking thread, while Popupwindow is blocking thread
Here's an example to illustrate the use of Popupwindow:
Activity.class:
1 Public classMainactivityextendsActivity {2 3 PrivateButton btn;4 5 @Override6 protected voidonCreate (Bundle savedinstancestate) {7 Super. OnCreate (savedinstancestate);8 Setcontentview (r.layout.activity_main);9BTN =(Button) Findviewbyid (r.id.show);TenBtn.setonclicklistener (NewButton.onclicklistener () { One A @Override - Public voidOnClick (View v) { -Popupwindow Popupwindow =NewPopupwindow (mainactivity. This); the //here is a ListView displayed in Popupwindow, you can also load a xx.xml file (Layoutfalter.from (). Inflate ()) -ListView ListView =NewListView (mainactivity. This); -arrayadapter<string> adapter =NewArrayadapter<string> (mainactivity. ThisAndroid. R.layout.simple_dropdown_item_1line,Newstring[]{"First", "second"}); - Listview.setadapter (adapter); +Popupwindow.setcontentview (ListView);//the view to be displayed in the Popupwindow -Popupwindow.setheight (500); +Popupwindow.setwidth (180); APopupwindow.setoutsidetouchable (true);//false By default outside of the Popupwindow range atPopupwindow.setbackgrounddrawable (NewColordrawable (Color.argb (50, 52, 53, 55))); - /** - * Popupwindow.showasdropdown (anchor, Xoff, Yoff) is used to mark where the Popupwindow is to be displayed - * Anchor: To the view as the standard location - * Xoff: The x offset at the base position of the anchor - * Yoff: The y offset at the base position of the anchor in */ -Popupwindow.showasdropdown (btn, 0, 0); to + } - }); the * } $ Panax Notoginseng @Override - Public BooleanOncreateoptionsmenu (Menu menu) { the getmenuinflater (). Inflate (R.menu.main, menu); + return true; A } the +}
The corresponding XML file contains a button that is not posted.
Source code Download: Source code