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 encapsulate PopupWindow

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to encapsulate PopupWindow". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "how to package PopupWindow"!

Overview

PopupWindow represents a pop-up window, which is similar to AlertDialog. Compared with AlertDialog, PopupWindow is more flexible to use, but it can specify any location to be displayed. Of course, flexible use must be sacrificed at a certain level. For example, PopupWindow has no default layout compared to AlertDialog, and you have to create a pop-up layout every time. AlertDialog is more convenient than AlertDialog, so there is no best solution in development. Choose the most appropriate solution according to your specific needs.

Common settin

Create a PopupWindow, as shown below:

/ / Construction method

Public PopupWindow (Context context)

Public PopupWindow (View contentView)

Public PopupWindow (View contentView, int width, int height)

Public PopupWindow (View contentView, int width, int height, boolean focusable)

Common attribute settings for PopupWindow, as shown below:

/ / set View (required)

Window.setContentView (contentView)

/ / set width (required)

Window.setWidth (WindowManager.LayoutParams.MATCH_PARENT)

/ / set high (required)

Window.setHeight (WindowManager.LayoutParams.WRAP_CONTENT)

/ / set the background

Window.setBackgroundDrawable (new ColorDrawable (Color.GRAY))

/ / set touch events other than PopupWindow

Window.setOutsideTouchable (true)

/ / set the listener whose PopupWindow disappears

Window.setOnDismissListener (this)

/ / set the touch event on PopupWindow

Window.setTouchable (true)

/ / animate the PopupWindow pop-up

Window.setAnimationStyle (R.style.PopupWindowTranslateTheme)

The display of PopupWindow can be set in two ways, one is based on coordinates, and the other is based on a certain View, as follows:

/ / based on coordinates, parameters (some View of the current window, location, starting coordinates x, starting coordinates y)

Void showAtLocation (View parent, int gravity, int x, int y)

/ / based on a View, parameters (offset in the attached View,x direction, offset in the y direction)

Void showAsDropDown (View anchor, int xoff, int yoff, int gravity)

Void showAsDropDown (View anchor, int xoff, int yoff)

Void showAsDropDown (View anchor)

Basic use

The main content of PopupWindow is basically like the above. The following uses native PopupWindow to implement a pop-up window, and the following is the key code, as follows:

/ / create a PopupWindow

PopupWindow window = new PopupWindow (this)

/ / set display View

Window.setContentView (contentView)

/ / set width and height

Window.setWidth (WindowManager.LayoutParams.MATCH_PARENT)

Window.setHeight (WindowManager.LayoutParams.WRAP_CONTENT)

/ / set the background

Window.setBackgroundDrawable (new ColorDrawable (Color.GRAY))

/ / set touch events other than PopupWindow

Window.setOutsideTouchable (true)

/ / set the listener whose PopupWindow disappears

Window.setOnDismissListener (new Popup_Window.OnDismissListener () {

@ Override

Public void onDismiss () {

/ / monitor the disappearance of PopupWindow

}

});

/ / set the touch event on PopupWindow

Window.setTouchable (true)

/ / animate the PopupWindow pop-up

Window.setAnimationStyle (R.style.PopupWindowTranslateTheme)

Window.showAtLocation (btnTarget, Gravity.BOTTOM | Gravity.CENTER, 0,0)

The following is the display effect, as follows:

Encapsulated PopupWindow

Here, the encapsulation of PopupWindow is mainly to further encapsulate the common placement location of PopupWindow, which makes the call of PopupWindow more flexible and concise.

The problem encountered in the packaging process is that the width and height of the PopupWindow can not be obtained correctly. The correct way to obtain the width and height is to measure the PopupWindow first, and then obtain its width and height, as shown below:

/ / get the width and height of PopupWindow

MPopupWindow.getContentView (). Measure

View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED)

View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED))

Int popupWidth = mPopupWindow.getContentView () .getMeasuredWidth ()

Int popupHeight = mPopupWindow.getContentView () .getMeasuredHeight ()

The builder design pattern is used for the encapsulation of PopupWindow. Let's take a look at the default configuration of PopupWindow as follows:

Public Builder (Context context) {

This.context = context

This.popupWindow = new PopupWindow (context)

/ / default PopupWindow responds to touch events

This.outsideTouchable = true

/ / default response to touch events

This.touchable = true

/ / the default background is transparent

This.backgroundDrawable = new ColorDrawable (Color.TRANSPARENT)

/ / default width and height is WRAP_CONTENT

This.width = WindowManager.LayoutParams.WRAP_CONTENT

This.height = WindowManager.LayoutParams.WRAP_CONTENT

/ / default Gravity is Gravity.CENTER

This.gravity = Gravity.CENTER

This.layoutId =-1

/ / the default offset is 0

This.offsetX = 0

This.offsetY = 0

/ /...

}

As the width and height, background, clickable and other related properties have been set the default value, when using according to their own needs to set relevant properties, such as PopupWindow animation, so these settings are definitely not necessary, then those things are necessary when creating.

The following is the initialization of the PopupWindow wrapper class MPopupWindow, as follows:

Private void setPopupWindowConfig (MPopupWindow window) {

If (contentView! = null & & layoutId! =-1) {

Throw new MException ("setContentView and setLayoutId can't be used together.", "0")

} else if (contentView = = null & & layoutId = =-1) {

Throw new MException ("contentView or layoutId can't be null.", "1")

}

If (context = = null) {

Throw new MException ("context can't be null.", "2")

} else {

Window.mContext = this.context

}

Window.mWidth = this.width

Window.mHeight = this.height

Window.mView = this.contentView

Window.mLayoutId = layoutId

Window.mPopupWindow = this.popupWindow

Window.mOutsideTouchable = this.outsideTouchable

Window.mBackgroundDrawable = this.backgroundDrawable

Window.mOnDismissListener = this.onDismissListener

Window.mAnimationStyle = this.animationStyle

Window.mTouchable = this.touchable

Window.mOffsetX = this.offsetX

Window.mOffsetY = this.offsetY

Window.mGravity = this.gravity

}

}

Obviously, you can see that context and contentView or layoutId must be set. If it is not set, there will be an error prompt. Of course, there is also a restriction in the encapsulation that contentView and layoutId cannot be used at the same time and an error prompt if both are used.

The following is the externally provided method for displaying PopupWindow, displaying PopupWindow in different locations according to different enumeration types, as follows:

Public void showPopupWindow (View v, LocationType type) {

If (mViewables null) {

MPopupWindow.setContentView (mView)

} else if (mLayoutId! =-1) {

View contentView = LayoutInflater.from (mContext) .predicate (mLayoutId, null)

MPopupWindow.setContentView (contentView)

}

MPopupWindow.setWidth (mWidth)

MPopupWindow.setHeight (mHeight)

MPopupWindow.setBackgroundDrawable (mBackgroundDrawable)

MPopupWindow.setOutsideTouchable (mOutsideTouchable)

MPopupWindow.setOnDismissListener (mOnDismissListener)

MPopupWindow.setAnimationStyle (mAnimationStyle)

MPopupWindow.setTouchable (mTouchable)

/ / obtain the coordinates of the target View

Int [] locations = new int [2]

V.getLocationOnScreen (locations)

Int left = locations [0]

Int top = locations [1]

/ / get the width and height of PopupWindow

MPopupWindow.getContentView (). Measure

View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED)

View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED))

Int popupWidth = mPopupWindow.getContentView () .getMeasuredWidth ()

Int popupHeight = mPopupWindow.getContentView () .getMeasuredHeight ()

Switch (type) {

Case TOP_LEFT:

MPopupWindow.showAtLocation (vGravity.NottingGRAVITY left-popupWidth + mOffsetX,top-popupHeight + mOffsetY)

Break

Case TOP_CENTER:

Int offsetX = (v.getWidth ()-popupWidth) / 2

MPopupWindow.showAtLocation (vGravity.NORVGRAVITY mPopupWindow.showAtLocation left + offsetX + mOffsetX,top-popupHeight + mOffsetY)

Break

Case TOP_RIGHT:

MPopupWindow.showAtLocation (vGravity.NORVGRAVITY mPopupWindow.showAtLocation left + v.getWidth () + mOffsetX,top-popupHeight + mOffsetY)

Break

Case BOTTOM_LEFT:

MPopupWindow.showAsDropDown (v,-popupWidth + mOffsetX,mOffsetY)

Break

Case BOTTOM_CENTER:

Int offsetX1 = (v.getWidth ()-popupWidth) / 2

MPopupWindow.showAsDropDown (vcenture offsetX1 + mOffsetX,mOffsetY)

Break

Case BOTTOM_RIGHT:

MPopupWindow.showAsDropDown (v, v.getWidth () + mOffsetX,mOffsetY)

Break

Case LEFT_TOP:

MPopupWindow.showAtLocation (v, Gravity.NO_GRAVITY, left-popupWidth + mOffsetX, top-popupHeight + mOffsetY)

Break

Case LEFT_BOTTOM:

MPopupWindow.showAtLocation (v, Gravity.NO_GRAVITY, left-popupWidth + mOffsetX, top + v.getHeight () + mOffsetY)

Break

Case LEFT_CENTER:

Int offsetY = (v.getHeight ()-popupHeight) / 2

MPopupWindow.showAtLocation (v, Gravity.NO_GRAVITY,left-popupWidth + mOffsetX,top + offsetY + mOffsetY)

Break

Case RIGHT_TOP:

MPopupWindow.showAtLocation (v, Gravity.NO_GRAVITY, left + v.getWidth () + mOffsetX,top-popupHeight + mOffsetY)

Break

Case RIGHT_BOTTOM:

MPopupWindow.showAtLocation (v, Gravity.NO_GRAVITY, left + v.getWidth () + mOffsetX,top + v.getHeight () + mOffsetY)

Break

Case RIGHT_CENTER:

Int offsetY1 = (v.getHeight ()-popupHeight) / 2

MPopupWindow.showAtLocation (v, Gravity.NO_GRAVITY,left + v.getWidth () + mOffsetX,top + offsetY1 + mOffsetY)

Break

Case FROM_BOTTOM:

MPopupWindow.showAtLocation (vGravitymOffsetX, mOffsetY)

Break

}

}

Use encapsulated PopupWindow

Using the encapsulated PopupWindow, it only takes four lines of code to display a default PopupWindow, as shown below:

Private void showPopupWindow (MPopup_Window.LocationType type) {

MPopupWindow popupWindow = new MPopupWindow

.Builder (this)

.setLayoutId (R.layout.popup_window_layout)

.build ()

PopupWindow.showPopupWindow (btnTarget, type)

}

Since the default PopupWindow background is transparent, it is recommended that you set the background when testing.

Display effect:

The following is the display of PopupWindow in various locations, as follows:

At this point, I believe you have a deeper understanding of "how to encapsulate PopupWindow". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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