Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use popupwindow in android

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to use popupwindow in android. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

I. basic usage

As a general practice, create a new class that inherits popupwindow. Example

/ * basic usage of 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) .expiate (R.layout.demographics basepoppopjold null); setContentView (view); setWidth (ViewGroup.LayoutParams.MATCH_PARENT); setHeight (200th); / / setHeight (ViewGroup.LayoutParams.MATCH_PARENT); setFocusable (true); setBackgroundDrawable (new BitmapDrawable ()); setTouchable (true); setOutsideTouchable (true) SetAnimationStyle (R.style.popwin_anim_style); / / setAnimationStyle (0); 0: no animation initView (view);} private void initView (View view) {dbp_text = (TextView) view.findViewById (R.id.dbp_text);}}

Study the popupwindow source code, in terms of 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(); //点击外部隐藏 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);}}

The system does this, and then comes the question of what to do if we want to monitor the physical return key. After looking at the above process, we can think of

SetBackgroundDrawable (null); then set the key snooping of view, and do the corresponding processing after listening. 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;}})

This is the end of the article on "how to use popupwindow in android". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report