1. What is a popWindow?
PopWindow is a dialog box! This article describes how to use the dialog box in android, called popWindow.
2. Features of popWindow
There are two types of Android dialogs: PopupWindow and AlertDialog. Their differences are:
The position of AlertDialog is fixed, and the position of PopupWindow can be random.
AlertDialog is a non-blocking thread, while PopupWindow is a blocking thread.
The position of PopupWindow can be divided into two types: Offset and no offset based on whether there is any offset. According to the difference of the reference object, it can be divided into a control (Anchor) and a parent control. The details are as follows:
ShowAsDropDown (View anchor): relative to a control (lower left), no offset
ShowAsDropDown (View anchor, int xoff, int yoff): The position of a widget with an offset.
ShowAtLocation (View parent, int gravity, int x, int y): The position relative to the parent control (for example, central Gravity. CENTER. Gravity. BOTTOM, etc.), you can set an offset or no offset
3. Design of popWindow
:
To achieve this effect, there are two xml la s, one is the toolbar layout, the other is the popWindow layout, and the other is the parent interface layout.
Toolbar layout:
PopWindow layout:
Parent interface layout:
The dialog box class inherits OnClickListener and corresponds 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); popupWindow = new PopupWindow (menu); popupWindow. setWidth (LayoutParams. MATCH_PARENT); popupWindow. setHeight (hight); popupWindow. setAnimationStyle (R. style. popAnimation);}/*** set the Menu button listener ** @ param mParent * @ return */private View initMenuView (Activity mParent) {View menuView = mParent. getLayoutInflater (). inflate (R. layout. my_simple_popupwindow_menu, null); menuView. find ViewById (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 case R. id. rlyt_menu: if (popupWindow! = Null & popupWindow. isShowing () {popupWindow. dismiss ();} break; // scan case R. id. rlyt_code: break; // history case R. id. rlyt_his: break;}/*** return View ** @ return */public PopupWindow getMenu () {return popupWindow ;}}
PopupWindow. setAnimationStyle (R. style. PopAnimation) in this Code; this line of code requires special attention. It acts on the pop-up effect of the dialog box and how to use my blog post that can be followed: publish. The usage principle 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 the first initialization pop-up box is 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 (); popMenu = 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) ;}}) ;}/ *** obtain the screen height ** @ param context * @ return */private int getScreenHeight (Context context) {WindowManager manager = (WindowManager) context. getSystemService (Context. WINDOW_SERVICE); Display display = manager. getdefadisplay display (); return display. getHeight () ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
Ps: The mIsFirst attribute of this Code is worth special attention. This attribute controls the number of pop-up times in the dialog box. If it is not controlled, it will pop up once every point.
The main code is almost the same. Click Download resource.