After repeated testing practice, the Popupwindow pop-up is displayed below the specified control. There are comments on the code, where you need to be aware. The Popupwindow has its own layout, and there are monitoring implementations of the controls inside. Next look at the code implementation.
Project Resources Download: Click here
Testpopwindow2.class
Package Com.example.popwindowdemo;import Android.app.actionbar.layoutparams;import Android.app.activity;import Android.content.context;import Android.graphics.drawable.bitmapdrawable;import Android.view.LayoutInflater; Import Android.view.view;import Android.widget.popupwindow;public class TestPopwindow2 extends Popupwindow {// Root View private View mrootview;//layoutinflaterlayoutinflater minflater;public TestPopwindow2 (Activity context) {super ( context); Minflater = (Layoutinflater) context.getsystemservice (context.layout_inflater_service); MRootView = Minflater.inflate (r.layout.test_popwindow_2, null); Setcontentview (Mrootview); This.setwidth (LayoutParams.WRAP_ CONTENT); This.setheight (layoutparams.wrap_content);//Set Popupwindow Popup related properties settouchable (true); Setoutsidetouchable (True); Setfocusable (true); Setbackgrounddrawable (New Bitmapdrawable (Context.getresources ())); Update (); Getcontentview (). Setfocusableintouchmode (True); Getcontentview (). Setfocusable (True); Setanimationstyle ( R.style.appbasetheme);}}
mainactivity. Class
Package Com.example.popwindowdemo;import Android.os.bundle;import Android.app.activity;import android.view.Gravity ; Import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.popupwindow.ondismisslistener;import Android.widget.toast;public class MainActivity extends Activity Implements Onclicklistener,ondismisslistener {private TestPopwindow2 mTestPopwindow2 = null; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); I Nitui ();} private void Initui () {//instantiation Testpopwindow2mtestpopwindow2 = new TestPopwindow2 (this);// Set Click other Location mTestPopwindow2 disappear Mtestpopwindow2.setondismisslistener (this); Button ButtonTest2 = (button) Findviewbyid (R.ID.BUTTONTEST2); Buttontest2.setonclicklistener (this);} private void OnPopwindowTest2 () {if (MTestPopwindow2 = = null) return;//location gets the position of the control int[] site = new INT[2]; View v = Findviewbyid (r.id.buttontest2); if (v! = null) v.getlOcationonscreen (location); The control Mtestpopwindow2.setanimationstyle (R.style.appbasetheme) in the position of the screen;//MTestPopwindow2 pops up in a control (button) The following mtestpopwindow2.showatlocation (V, gravity.top | Gravity.left,location[0]-v.getwidth (), location[1] + v.getheight ());} The MTestPopwindow2 layout control listens to public void Onclicktestlistener (view view) {switch (View.getid ()) {r.id.layoutseclect1: Toast.maketext (This, "system popular Scenario", Toast.length_short). Show (); Break;case R.id.layoutseclect2:toast.maketext (This, " Personal popular program ", Toast.length_short). Show (); break;default:break;} if (mTestPopwindow2! = null) Mtestpopwindow2.dismiss ();} @Overridepublic void OnClick (View v) {switch (V.getid ()) {case r.id.buttontest2:onpopwindowtest2 (); Break;default: Break;}} Click somewhere else to disappear @overridepublic void Ondismiss () {}}
test_popwindow_2. xml
<?xml version= "1.0" encoding= "UTF-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:background=" #0000 "> <!--Popupwimdow layout Note You cannot use a property like Android:layout_margintop= "", especially relative to the layout time. Layout or control overall left and so on will affect Popupwimdow positioning use--<linearlayout android:id= "@+id/hot_layout" Android:layout_wid Th= "Wrap_content" android:layout_height= "wrap_content" android:background= "@drawable/title_function_bg" android:orientation= "vertical" > <linearlayout android:id= "@+id/layoutseclect1" Android Oid:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:clickable= "true" android:onclick= "Onclicktestlistener" > <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:padding= "8DP" android:text= "system popular Scenario" android:textcolor= "#fff" Android : textsize= "18sp"/> </LinearLayout> <linearlayout android:id= "@+id/layoutseclect2" Android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:clickable= "True" android:onclick= "Onclicktestlistener" > <textview android:layout_width= " Wrap_content "android:layout_height=" wrap_content "android:padding=" 8DP "Android oid:text= "Personal hot Plan" android:textcolor= "#fff" android:textsize= "18sp"/> </linearl Ayout> </LinearLayout></LinearLayout>
Activity_main.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:id=" @+id/linearlayoutmain " android:layout_width=" Match_parent " android:layout_height= "Match_parent" android:orientation= "vertical" > <textview android: Layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/hello_world"/ > <linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:gravity= "Right" > <button android:id= "@+id/buttontest2" android:layout_width = "Wrap_content" android:layout_height= "wrap_content" android:text= "Test 2"/> </linearlayout ></LinearLayout>
as follows
Project Resources Download: Click here
Reprint please indicate the source of the blog site: http://blog.csdn.net/qq_16064871
If there is a reprint does not indicate the source of the blog URL, or did not get the author's consent. The author holds copyright ownership.
Android Popupwindow Sample Program One