1. What is Popwindow?
Popwindow This is the way of dialogue! The text explains the way Android uses the dialog box, which is called Popwindow.
2. Popwindow characteristics
There are two types of dialog boxes for Android: Popupwindow and dialog. They differ in the following points:
The position of the dialog is fixed, while the position of the Popupwindow can be arbitrary.
Dialog is a non-clogging thread, while Popupwindow is a plug-in thread.
The position of the Popupwindow can be divided into offset and non-offset according to the deviation, and can be divided into relative to a control (anchor anchor) and relative to the parent control, depending on the subject matter.
Details such as the following
Showasdropdown (View anchor): relative to the position of a control (just below the left), no offset
Showasdropdown (View anchor, int xoff, int yoff): Offset relative to the position of a control
Showatlocation (View parent, int gravity, int x, int y): The position relative to the parent control, such as the central Gravity.center. Lower Gravity.bottom, etc.), able to set offset or no offset
3, the design of Popwindow
:
To achieve this, there is a total of two three XML layouts, one for the toolbar layout. One is the Popwindow layout, and one is the parent interface layout.
Toolbar layout:
<?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=" @dimen/chat_llyt_title_height "Android:layout_ Alignparentleft= "true" android:layout_alignparenttop= "true" android:background= "@color/black" android:orientation = "Horizontal" > <imageview android:id= "@+id/app_iv_back" android:layout_width= "@dimen/chat_llyt _title_height "android:layout_height=" fill_parent "android:background=" @drawable/actionbar_bg_left "a ndroid:layout_gravity= "center" android:padding= "7dip" android:scaletype= "Centerinside"/> < TextView android:id= "@+id/app_tv_barname" android:layout_width= "0.0dip" android:layout_height= "fill_p Arent "android:layout_weight=" 4.0 "android:background=" @drawable/actionbar_bg_middle "android:gravity = "Center" android:text= "@strIng/app_name "android:textcolor=" @color/white "android:textsize=" @dimen/wc_title_textsize "/> <Imag Eview android:id= "@+id/app_iv_search" android:layout_width= "@dimen/chat_llyt_title_height" Android:la yout_height= "Fill_parent" android:background= "@drawable/actionbar_bg_right" android:padding= "7dip" an droid:layout_gravity= "center" android:scaletype= "Centerinside" android:src= "@drawable/yuyou_bg_pop"/>< ;/linearlayout>
Popwindow layout:
<?xml version= "1.0" encoding= "UTF-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:id=" @+id/rlyt_menu "android:layout_width=" match_parent "android:layout_height=" Match_parent " android:orientation= "Horizontal" > <view android:layout_width= "0dp" android:layout_height= "0DP" android:layout_weight= "9"/> <linearlayout android:layout_width= "0DP" android:layout_height= "Ma Tch_parent "android:layout_weight=" 2 "android:orientation=" vertical "> <view Android : layout_width= "0DP" android:layout_height= "0DP" android:layout_weight= "0.05"/> <linear Layout android:layout_width= "match_parent" android:layout_height= "0DP" Android:layout_wei Ght= "2.3" android:background= "@drawable/yuyou_pop_menu" android:orientation= "vertical" > <view android:Layout_width= "Match_parent" android:layout_height= "0DP" android:layout_weight= "0.1"/> <linearlayout android:id= "@+id/rlyt_code" android:layout_width= "Match_parent" android:layout_height= "0DP" android:layout_weight= "1" android:orientation= "vert ical "> <imageview android:layout_width=" match_parent "Android: layout_height= "0DP" android:layout_weight= "1" android:contentdescription= "@drawable/ FACE_CODE_BG "android:paddingleft=" 10DP "android:paddingright=" 10DP " android:src= "@drawable/face_code_bg" android:scaletype= "Centerinside"/> <text View android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android:graVity= "Center_horizontal|top" android:text= "Sweep" android:textcolor= "@color/white" Android:textsize= "@dimen/yuyou_pop_text_size"/> </LinearLayout> <view Android:layout_width= "0DP" android:layout_height= "0DP" android:layout_weight= " 0.05 "android:visibility=" Gone "/> <linearlayout android:id=" @+id/rlyt_his " Android:layout_width= "Match_parent" android:layout_height= "0DP" Android:layo ut_weight= "1" android:orientation= "vertical" > <imageview android:l Ayout_width= "Match_parent" android:layout_height= "0DP" android:layout_weight= "1" android:contentdescription= "@drawable/face_his_bg" android:paddingleft= "10DP" Android:paddingright= "10DP" android:src= "@drawable/face_his_bg" android:scaletype= "Centerinside" /> <textview android:id= "@+id/tv_ting_tong" Android:layout_widt H= "Match_parent" android:layout_height= "wrap_content" android:gravity= "Center_horizo Ntal|top "android:text=" History "android:textcolor=" @color/white "an Droid:textsize= "@dimen/yuyou_pop_text_size"/> </LinearLayout> <view Android Oid:layout_width= "0DP" android:layout_height= "0DP" android:layout_weight= "0.05" android:visibility= "Gone"/> </LinearLayout> <view android:layout_width= "0DP" android:layout_height= "0DP" android:layout_weight= "6"/> </LinearLayout> <view Android:layout_width= "0dP "android:layout_height=" 0DP "android:layout_weight=" 0.05 "/></linearlayout>
Parent Interface Layout:
<?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=" @dimen/chat_llyt_title_height "Android:layout_ Alignparentleft= "true" android:layout_alignparenttop= "true" android:background= "@color/black" android:orientation = "Horizontal" > <imageview android:id= "@+id/app_iv_back" android:layout_width= "@dimen/chat_llyt _title_height "android:layout_height=" fill_parent "android:background=" @drawable/actionbar_bg_left "a ndroid:layout_gravity= "center" android:padding= "7dip" android:scaletype= "Centerinside"/> < TextView android:id= "@+id/app_tv_barname" android:layout_width= "0.0dip" android:layout_height= "fill_p Arent "android:layout_weight=" 4.0 "android:background=" @drawable/actionbar_bg_middle "android:gravity = "Center" android:text= "@strIng/app_name "android:textcolor=" @color/white "android:textsize=" @dimen/wc_title_textsize "/> <Imag Eview android:id= "@+id/app_iv_search" android:layout_width= "@dimen/chat_llyt_title_height" Android:la yout_height= "Fill_parent" android:background= "@drawable/actionbar_bg_right" android:padding= "7dip" an droid:layout_gravity= "center" android:scaletype= "Centerinside" android:src= "@drawable/yuyou_bg_pop"/>< ;/linearlayout>
Dialog class, inheriting Onclicklistener. corresponding to the Click event:
Package Com.example.popwindow;import Android.app.activity;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup.layoutparams;import Android.widget.PopupWindow; public class Mysimplepopmenu implements Onclicklistener {private final Popupwindow popupwindow;private Activity mparent; Public Mysimplepopmenu (Activity mparent, int hight) {this.mparent = mparent; View menu = Initmenuview (mparent);p Opupwindow = new Popupwindow (menu);p opupwindow.setwidth (layoutparams.match_parent );p Opupwindow.setheight (hight);p Opupwindow.setanimationstyle (r.style.popanimation);} /** * Set VERBOSE menubutton monitoring * * @param mparent * @return */private view Initmenuview (Activity mparent) {View Menuview = Mpare Nt.getlayoutinflater (). Inflate (R.layout.my_simple_popupwindow_menu, null); Menuview.findviewbyid (R.id.rlyt_menu) . Setonclicklistener (this); Menuview.findviewbyid (R.id.rlyt_code) Setonclicklistener (this); Menuview.findviewbyid (r.id.rlyt_his). Setonclicklistener (this); return Menuview;} @Overridepublic void OnClick (view view) {switch (View.getid ()) {//Click Space Case r.id.rlyt_menu:if (Popupwindow! = null &&am P Popupwindow.isshowing ()) {Popupwindow.dismiss ();} break;//Sweep Case r.id.rlyt_code:break;//history case r.id.rlyt_his:break;}} /** * Return to view * * @return */public Popupwindow getmenu () {return popupwindow;}}
Popupwindow.setanimationstyle in this code (r.style.popanimation), this line of code need to pay special attention to the popup effect for the dialog box, how to use the attention of my blog:/http blog.csdn.net/stoppig/article/details/21481057. The principle of use is almost identical.
Main Activity code:
Package Com.example.popwindow;import Android.os.bundle;import Android.app.activity;import android.content.Context; Import Android.view.display;import Android.view.gravity;import Android.view.layoutinflater;import Android.view.menu;import Android.view.view;import Android.view.windowmanager;import Android.view.view.onclicklistener;import Android.widget.imageview;import Android.widget.linearlayout;import Android.widget.popupwindow;import Android.widget.relativelayout;public class Mainactivity extends Activity {private Mysimplepopmenu popmenu;//pop-up selection box private Popupwindow Mpopupwindow; Pop-up box private ImageView app_iv_search;private Boolean misfirst = true; Whether to initialize the popup box for the first time private Context mcontext;private relativelayout mlayout; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); FindViews (); Initlistener (); this.mcontext = this;} private void Findviews () {App_iv_search = (ImageView) Findviewbyid (r.id.app_iv_search); MLAyout = (relativelayout) Findviewbyid (R.id.rv_main);} private void Initlistener () {App_iv_search.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick ( View v) {//TODO auto-generated method stubif (misfirst) {Misfirst = false;int height = mlayout.getheight ()-V.getheight ( );p Opmenu = new Mysimplepopmenu (mainactivity.this, height); Mpopupwindow = Popmenu.getmenu ();} if (Mpopupwindow = = null) {return;} if (mpopupwindow.isshowing ()) {Mpopupwindow.dismiss (); return;} Mpopupwindow.showatlocation (mlayout, gravity.bottom, 0, 0);}}); /** * Get Screen Height * * @param context * @return */private int Getscreenheight (context context) {WindowManager manager = (Window Manager) Context.getsystemservice (context.window_service);D isplay Display = Manager.getdefaultdisplay (); return Display.getheight ();} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}
PS: This piece of code Misfirst This attribute is especially important. This property controls the number of times a dialog box pops up, assuming there is no control. Each point will pop up once.
The main code is almost identical. Resource address Please click: Download resources.
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
Android in Popwindow write popup menu