Package Com.example.popupwindowdemo01;import Android.app.activity;import Android.graphics.color;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.ViewGroup ; Import Android.widget.button;import Android.widget.linearlayout;import Android.widget.popupwindow;import Android.widget.textview;public class Mainactivity extends Activity implements Onclicklistener {private Button Main_ button, @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);//Note the original layout// Setcontentview (R.layout.activity_main);//Instantiate layout linearlayout llayout = new LinearLayout (this);// Set layout Direction llayout.setorientation (linearlayout.vertical);//Add to Activity This.setcontentview (llayout);//Instantiate button Main_ button = New button (this),///Set button text Main_button.settext ("Show Popu"),//Add buttons to layout Llayout.addview (Main_button);// Add Click event Main_button.setonclicklistener (this) for the button;} @Overridepublic void OnClick (View arg0) {//TODO auto-generated method stub//instantiation OneTextviewtextview v = new TextView (this);///NOTE: Make sure that the view has a high width, or you can set the text so that the view passive settings aspect//can also be manually written v.setheight (50); v.setwidth ); V.settext ("I am a button"); V.setbackgroundcolor (color.green);//Instantiate popupwindow//the first parameter in this is a view, As long as the view and has a high width can//two, three parameters is popupwindow high width, not the first parameter view of the high width popupwindow pp = new Popupwindow (V, viewgroup.layoutparams.match_parent,viewgroup.layoutparams.wrap_content), or//appears under the button, can be set other position Pp.showasdropdown ( Main_button);}}
Run:
Tenth chapter, Popupwindow simple Demo (Android)