Android Development Series (26): Create a dialog-style window with Popupwindow

Source: Internet
Author: User

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

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.