Android popupwindow pop-up tutorial

Source: Internet
Author: User

From: http://www.cyqdata.com/android/article-detail-25069

In Android development, a window may pop up from time to time. This article introduces you to the pop-up window of Android popupwindow. In fact, the pop-up window of Android popupwindow is similar to the alertdialog dialog box, this article first introduces the content of the pop-up window of Android popupwindow. See the tutorial below:

First, initialize an android popupwindow.

PopupWindow  mPop = new PopupWindow(getLayoutInflater().inflate(R.layout.window, null),    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

Implement the popupwindow constructor. The first parameter is used to import the layout. The next two Parameters specify the width and height. Pop-up windows can be displayed in two ways: showasdropdown () and showatlocation. There are two common parameters: Offset and no offset.

Bt1.setonclicklistener (New onclicklistener () {@ override public void onclick (view v) {initpopwindow (); MPOP. showasdropdown (V); // use this button as anchor (which can be understood as an anchor or benchmark), and pop up at the bottom}); bt2.setonclicklistener (New onclicklistener () {@ override public void onclick (view v) {initpopwindow (); MPOP. showasdropdown (v, 20,-20); // horizontal axis offset 20, vertical axis-20, length of a status bar}); bt3.setonclicklistener (New onclicklistener () {@ override public void oncl Ick (view v) {initpopwindow (); MPOP. showatlocation (popwindow. this. findviewbyid (R. id. RL), gravity. center, 0, 0); // center the screen without offset}); bt4.setonclicklistener (New onclicklistener () {@ override public void onclick (view v) {initpopwindow (); MPOP. showatlocation (popwindow. this. findviewbyid (R. id. RL), gravity. top | gravity. left, 20, 20); // on the top of the screen | right, with offset}); bt5.setonclicklistener (New onclicklistener () {@ override Public void onclick (view v) {If (MPOP! = NULL) {MPOP. Dismiss ();}}});

The initpopwindow () method is used to initialize a pop-up window.

 private void initPopWindow() {        if (mPop == null) {             mPop = new PopupWindow(getLayoutInflater().inflate(R.layout.pop, null),                     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);         }         if (mPop.isShowing()) {            mPop.dismiss();         }     }

Four buttons are defined here to display the effect. One button is used to close the display. Let's see the effect of the third button. You can try it yourself.

My friends who have read this series of blogs will find that the layout in the popupwindow is the layout in the drawer, and the background behind it is the Extensible images described in the previous blog. Why is this used? Let's take a look at the menu Effect of UC.

This may be done using alertdialog, but it is very convenient to use popupwindow. Many applications use such menus. Here I will not do it like UC, just use the layout above, the principle is the same, just change the picture.

It's easy to do. Check the code.

Public Boolean onkeydown (INT keycode, keyevent event) {// intercept key event if (keycode = keyevent. keycode_menu) {initpopwindow (); MPOP. showatlocation (this. findviewbyid (R. id. RL), gravity. bottom | gravity. center_horizontal, 0, 0); // at the bottom of the screen} else if (keycode = keyevent. keycode_back) {If (MPOP. isshowing () {MPOP. dismiss ();} else {system. exit (0) ;}} return false ;}

View the effect

The image does not seem to be very powerful. You can just make some beautiful pictures.

I posted all the code.

1 package COM. notice. popwindow; 2 3 Import android. app. activity; 4 Import android. OS. bundle; 5 import android. view. gravity; 6 Import android. view. keyevent; 7 Import android. view. view; 8 Import android. view. view. onclicklistener; 9 Import android. view. viewgroup. layoutparams; 10 Import android. widget. button; 11 import android. widget. gridview; 12 Import android. widget. popupwindow; 13 14 public class Po Pwindow extends activity {15 private gridview GV; 16 private button Bt1; 17 private button bt2; 18 private button bt3; 19 private button bt4; 20 private button BT5; 21 private int [] icons = {R. drawable. browser, R. drawable. gallery, 22 R. drawable. camera, R. drawable. gmail, 23 R. drawable. music, R. drawable. market, 24 R. drawable. phone, R. drawable. messages, R. drawable. maps}; 25 private string [] items = {"Browser", "figure Slice "," camera "," Clock "," Music "," market "," dialing "," information "," map "}; 26 private popupwindow MPOP; 27 private view layout; 28 private void initpopwindow () {29 If (MPOP = NULL) {30 MPOP = new popupwindow (layout, 31 layoutparams. wrap_content, layoutparams. wrap_content); 32} 33 If (MPOP. isshowing () {34 MPOP. dismiss (); 35} 36} 37/** called when the activity is first created. */38 @ override 39 public void oncreate (bundle Savedinstancestate) {40 super. oncreate (savedinstancestate); 41 setcontentview (R. layout. main); 42 Bt1 = (button) findviewbyid (R. id. bt1); 43 bt2 = (button) findviewbyid (R. id. bt2); 44 bt3 = (button) findviewbyid (R. id. bt3); 45 bt4 = (button) findviewbyid (R. id. bt4); 46 BT5 = (button) findviewbyid (R. id. BT5); 47 layout = view. inflate (this, R. layout. window, null); 48 GV = (gridview) layout. findviewbyid (R. id. g V); 49 myadapter adapter = new myadapter (this, items, icons); 50 GV. setadapter (adapter); 51 52 bt1.setonclicklistener (New onclicklistener () {53 54 @ override 55 public void onclick (view v) {56 initpopwindow (); 57 MPOP. showasdropdown (V); // use this button as anchor (which can be understood as an anchor, as a benchmark). 58 59 }}is displayed below. 60 61 bt2.setonclicklistener (New onclicklistener () {62 63 @ override 64 public void onclick (view v) {65 initpopwindow (); 66 MPOP. showasdropdown (v, 20,-20); // horizontal axis offset 20, vertical axis-20, length of a status bar 67 68}); 69 70 bt3.setonclicklistener (New onclicklistener () {71 72 @ override 73 public void onclick (view v) {74 initpopwindow (); 75 MPOP. showatlocation (popwindow. this. findviewbyid (R. id. RL), 76 gravity. center, 0, 0); // center the screen, no offset 77 78}); 79 80 81 bt4.setonclicklistener (New onclicklistener () {82 83 @ override 84 public void onclick (view V) {85 initpopwindow (); 86 MPOP. showatlocation (popwindow. this. findviewbyid (R. id. RL), 87 gravity. top | gravity. left, 20, 20); // on the top of the screen | right, with an offset of 88 89}); 90 91 bt5.setonclicklistener (New onclicklistener () {92 93 @ override 94 public void onclick (view v) {95 If (MPOP! = NULL) {96 MPOP. dismiss (); 97} 98 99}); 100 101} 102 public Boolean onkeydown (INT keycode, keyevent event) {103 // capture key event 104 If (keycode = keyevent. keycode_menu) {105 initpopwindow (); 106 MPOP. showatlocation (this. findviewbyid (R. id. RL), 107 gravity. bottom | gravity. center_horizontal, 0, 0); // at the bottom of the screen 108} else if (keycode = keyevent. keycode_back) {109 If (MPOP. isshowing () {110 MPOP. dismiss (); 111} else {112 system. exit (0); 113} 114 115} 116 return false; 117 118} 119}

The myadapter class can be used to view my previous blog writing a drawer. The code is in it. Then window. xml

<?xml version="1.0" encoding="utf-8"?> <GridView        xmlns:android="http://schemas.android.com/apk/res/android"      android:id="@+id/gv"       android:background="@drawable/tbg"      android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:numColumns="3"      android:gravity="center"> </GridView> 

There is only one gridview. Main. xml won't be pasted, just a few buttons.

 

 

 

 

Related Article

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.