Introduced
Android needs to pop like this will be used PopupWindow, so I do this encapsulates PopupWindow tools,
Use Example
Each with a different effect:
Displayed above the button
View inflate1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.pop_item, null, false);
PopUtils popUtils1 = new PopUtils (v, inflate1);
popUtils1.showPop();
Button 100 displayed in the offset direction, down to negative to
View inflate2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.pop_item, null, false);
PopUtils popUtils2 = new PopUtils (v, inflate2);
popUtils2.showPopY(100);
Left shift button 100 displays, to the right to negative
View inflate3 = LayoutInflater.from(MainActivity.this).inflate(R.layout.pop_item, null, false);
PopUtils popUtils3 = new PopUtils (v, inflate3);
popUtils3.showPopX(100);
Offset up button 100 displays, display 100 offset to the left, the opposite will be changed to negative
View inflate4 = LayoutInflater.from(MainActivity.this).inflate(R.layout.pop_item, null, false);
PopUtils popUtils4 = new PopUtils (v, inflate4);
popUtils4.showPopXY(100, 100);
Related explanation
// Get View, R.layout.xxx is to show the contents PopupWindow
View inflate = LayoutInflater.from(上下文Context).inflate(R.layout.xxx, null, false);
// write here PopupWindow the controls related events
...
// instantiate
// v: Tap / long press event footage back View
// inflate: PopupWindow to show the View
PopUtils popUtils = new PopUtils (v, inflate);
// display PopupWindow
popUtils.showPop();
Packaging tools
Some considerations integrated package
PopupWindow defined window position
According View pop-up event location
/**
* @author ThirdGoddess
* @email [email protected]
* @Github https://github.com/ThirdGoddess
* @date :2020-03-17 01:42
*/
public class PopUtils {
private View v;
private PopupWindow popupWindow;
private int[] location;
private int popupWidth;
private int popupHeight;
/**
* @Param v View event
* @Param inflate bomb box item
*/
PopUtils(View v, View inflate) {
this.v = v;
popupWindow = new PopupWindow(inflate, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
inflate.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
popupWidth = inflate.getMeasuredWidth();
popupHeight = inflate.getMeasuredHeight();
location = new int[2];
v.getLocationOnScreen(location);
}
/**
* PopupWindow appears above the event View
*/
public void showPop () { Zhengzhou gynecological hospital http://www.0371zzkd.com/
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - popupWidth / 2, location[1] - popupHeight);
}
/**
Y-axis offset *
*
* @Param offsetY Y axis offset (positive on the move, negative down)
*/
public void showPopY(int offsetY) {
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - popupWidth / 2, location[1] - popupHeight - offsetY);
}
/**
The x-axis offset *
*
* @Param offsetX X axis offset (positive left, right negative)
*/
public void showPopX(int offsetX) {
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - popupWidth / 2 - offsetX, location[1] - popupHeight);
}
/**
* X-axis and y-axis offset
*
* @Param offsetX X axis offset (positive left, right negative)
* @Param offsetY Y axis offset (positive on the move, negative down)
*/
public void showPopXY(int offsetX, int offsetY) {
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - popupWidth / 2 - offsetX, location[1] - popupHeight - offsetY);
}
/**
* Close PopupWindow
*/
public void dismissPop() {
if (null != popupWindow) {
popupWindow.dismiss();
}
}
}