Creating a dialog-style window is simple and requires steps:
1. Call the Popupwindow constructor to create the Popupwindow object
2. The Showasdropdown (View v) called Popupwindow is displayed as a drop-down component of the V component: or call Popupwindow's Showatlocation () method to display the Popupwindow at the specified location.
First, we create an Android project and then edit the Main.xml file:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " Android:gravity= "Center_horizontal" ><button android:id= "@+id/bn" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Popup popup Window"/></linearlayout>
We have defined a button to open the Popup dialog style window
Then, we edit the Java code in the main interface: Popupwindowtest.java
Import Android.app.activity;import android.os.bundle;import Android.view.gravity;import Android.view.View;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.popupwindow;public Class Popupwindowtest extends activity{@Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.main);//load R.layout.popup corresponding interface layout view root = This.getlayoutinflater (). Inflate (r.layout.popup, NULL);//Create Popupwindow object final Popupwindow popup = new Popupwindow (root, 280, 360); Button button = (button) Findviewbyid (r.id.bn), Button.setonclicklistener (new Onclicklistener () {@Overridepublic void The OnClick (View v) {//drop-down mode is displayed. Popup.showasdropdown (v);//Displays Popupwindow in the specified position popup.showatlocation (Findviewbyid (r.id.bn), Gravity.center, 20,20 );}});/ /Gets the Close button in the Popupwindow. Root.findviewbyid (R.id.close). Setonclicklistener (New View.onclicklistener () {public void OnClick (View v) {// Close Popupwindowpopup.dismiss (); Responsible for destroying, hiding Popupwindow key Code});}}
We also wrote an XML file for the Popupwindow dialog box: popup.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " Android:gravity= "Center_horizontal" android:background= "#B1C9E4" ><imageview android:layout_width= "240DP" android:layout_height= "Wrap_content" android:src= "@drawable/java"/><buttonandroid:id= "@+id/close" Android: Layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Off"/></linearlayout>
Let's take a look at the effect:
Android Development Series (26): Create a dialog-style window using Popupwindow