android popupwindow用法詳解
本文實例為大家分享了android popupwindow的用法,供大家參考,具體內(nèi)容如下
一、基本用法
一般做法,新建類繼承popupwindow。例
/** * popupwindow基本用法 * Created by Administrator on 2015/11/25. */ public class DemoBasePop extends PopupWindow { private LinearLayout linear_layout; private TextView dbp_text; private Context context; public DemoBasePop(final Activity context) { super(context); this.context = context; View view = LayoutInflater.from(context).inflate(R.layout.demo_base_pop,null); setContentView(view); setWidth(ViewGroup.LayoutParams.MATCH_PARENT); setHeight(200); // setHeight(ViewGroup.LayoutParams.MATCH_PARENT); setFocusable(true); setBackgroundDrawable(new BitmapDrawable()); setTouchable(true); setOutsideTouchable(true); setAnimationStyle(R.style.popwin_anim_style); // setAnimationStyle(0); 0是沒有animation initView(view); } private void initView(View view) { dbp_text = (TextView) view.findViewById(R.id.dbp_text); } }
研究下popupwindow源碼,以showAsDropDown來講
public void showAsDropDown(View anchor, int xoff, int yoff) { if (isShowing() || mContentView == null) { return; } registerForScrollChanged(anchor, xoff, yoff); mIsShowing = true; mIsDropdown = true; WindowManager.LayoutParams p = createPopupLayout(anchor.getWindowToken()); preparePopup(p); updateAboveAnchor(findDropDownPosition(anchor, p, xoff, yoff)); if (mHeightMode < 0) p.height = mLastHeight = mHeightMode; if (mWidthMode < 0) p.width = mLastWidth = mWidthMode; p.windowAnimations = computeAnimationResource(); invokePopup(p); }
第11行創(chuàng)建WindowManager.LayoutParams。第12行preparePopup()中:
if (mBackground != null) { final ViewGroup.LayoutParams layoutParams = mContentView.getLayoutParams(); int height = ViewGroup.LayoutParams.MATCH_PARENT; if (layoutParams != null && layoutParams.height == ViewGroup.LayoutParams.WRAP_CONTENT) { height = ViewGroup.LayoutParams.WRAP_CONTENT; } // when a background is available, we embed the content view // within another view that owns the background drawable PopupViewContainer popupViewContainer = new PopupViewContainer(mContext); PopupViewContainer.LayoutParams listParams = new PopupViewContainer.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, height ); popupViewContainer.setBackgroundDrawable(mBackground); popupViewContainer.addView(mContentView, listParams); mPopupView = popupViewContainer; } else { mPopupView = mContentView; }
如果做了setBackgroundDrawable(new BitmapDrawable());那么mBackground則不為空,則會用PopupViewContainer作為mPopupView(即內(nèi)容view)。而PopupViewContainer的dispatchKeyEvent對返回鍵做了處理,按返回鍵后其中調(diào)用dismiss()方法。其onTouchEvent對觸摸事件做了處理,其源碼:
public boolean onTouchEvent(MotionEvent event) { final int x = (int) event.getX(); final int y = (int) event.getY(); <span style="font-family: 宋體; font-size: 9pt;">//點擊外部隱藏</span> if ((event.getAction() == MotionEvent.ACTION_DOWN) && ((x < 0) || (x >= getWidth()) || (y < 0) || (y >= getHeight()))) { dismiss(); return true; } else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) { dismiss(); return true; } else { return super.onTouchEvent(event); } }
系統(tǒng)做了這些處理,隨之而來一個問題,如果我們要監(jiān)聽物理返回鍵該怎么辦??戳松厦娴倪^程,我們可以想到將
setBackgroundDrawable(null);然后通過設(shè)置view的key監(jiān)聽,監(jiān)聽到后做相應(yīng)的處理。 view.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) { outAnimator.start(); return true; } } return false; } });
效果圖:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)工程中集成mob短信驗證碼功能的方法
這篇文章主要介紹了Android開發(fā)工程中集成mob短信驗證碼功能的方法,感興趣的小伙伴們可以參考一下2016-05-05