您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關android中popupwindow怎么用,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
一、基本用法
一般做法,新建類繼承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行創建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(即內容view)。而PopupViewContainer的dispatchKeyEvent對返回鍵做了處理,按返回鍵后其中調用dismiss()方法。其onTouchEvent對觸摸事件做了處理,其源碼:
public boolean onTouchEvent(MotionEvent event) { final int x = (int) event.getX(); final int y = (int) event.getY(); <span >//點擊外部隱藏</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); } }
系統做了這些處理,隨之而來一個問題,如果我們要監聽物理返回鍵該怎么辦。看了上面的過程,我們可以想到將
setBackgroundDrawable(null);然后通過設置view的key監聽,監聽到后做相應的處理。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; } });
關于“android中popupwindow怎么用”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。