Popwindow Pop-up menu authoring in Android

Source: Internet
Author: User

1. What is Popwindow?

Popwindow is a way of dialog boxes! This article explains the use of the dialog box in Android, which is called Popwindow.

2, the characteristics of Popwindow

There are two types of dialog boxes for Android: Popupwindow and Alertdialog. They differ in the following points:
The position of the alertdialog is fixed, while the position of the Popupwindow can be arbitrary.
Alertdialog are non-blocking threads, while popupwindow are blocking threads.
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 reference. Specific as follows
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): can be set offset or no offset relative to the parent control's position (for example, central Gravity.center, Lower gravity.bottom, etc.)

3, the design of Popwindow



To achieve this, there are two three XML layouts in total, one for the toolbar layout, one for the Popwindow layout, and one for 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"/>&lt ;/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"/>&lt ;/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 specific menu button for listening * * @param mparent * @return */private view Initmenuview (Activity mparent) {View Menuview = Mparent.g Etlayoutinflater (). 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 && P Opupwindow.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 needs special attention, for the popup Effect of the dialog box, how to use the attention of my blog:/http blog.csdn.net/stoppig/article/details/21481057. The principle of use is similar.


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 code misfirst This property is special attention, this property controls the number of times the dialog box pops up, if not control, each point will pop up once.


The main code is almost there. Resource address Please click: Download resources.



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.