Android 自定义PopupWindow指定位置或给定View坐标弹出
来源:互联网 发布:finereport 数据库 编辑:程序博客网 时间:2024/04/30 13:17
PopupWindowHelper是一个根据View的位置显示PopupWindow的一个工具类、弹出PopupWindow的位置会根据指定的View出现的位置而动态改变、本Demo是用Android Studio开发的、分为两个Module、但我个人更习惯合到一起、所以看个人需要了、代码很简单、就一个PopupWindowHelper工具类、然后直接可以在Activity上面使用了、效果如下:
PopupWindowHelper代码
public class PopupWindowHelper { private View popupView; private PopupWindow mPopupWindow; private static final int TYPE_WRAP_CONTENT = 0, TYPE_MATCH_PARENT = 1; public PopupWindowHelper(View view) { popupView = view; } public void showAsDropDown(View anchor) { mPopupWindow.showAsDropDown(anchor); } public void showAsDropDown(View anchor, int xoff, int yoff) { initPopupWindow(TYPE_WRAP_CONTENT); mPopupWindow.showAsDropDown(anchor, xoff, yoff); } public void showAtLocation(View parent, int gravity, int x, int y) { initPopupWindow(TYPE_WRAP_CONTENT); mPopupWindow.showAtLocation(parent, gravity, x, y); } public void dismiss() { if (mPopupWindow.isShowing()){ mPopupWindow.dismiss(); } } public void showAsPopUp(View anchor) { showAsPopUp(anchor, 0, 0); } public void showAsPopUp(View anchor, int xoff, int yoff) { initPopupWindow(TYPE_WRAP_CONTENT); mPopupWindow.setAnimationStyle(R.style.AnimationUpPopup); popupView.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); int height = popupView.getMeasuredHeight(); int[] location = new int[2]; anchor.getLocationInWindow(location); mPopupWindow.showAtLocation(anchor, Gravity.LEFT | Gravity.TOP, location[0] xoff, location[1] - height yoff); } public void showFromBottom(View anchor) { initPopupWindow(TYPE_MATCH_PARENT); mPopupWindow.setAnimationStyle(R.style.AnimationFromButtom); mPopupWindow.showAtLocation(anchor, Gravity.LEFT | Gravity.BOTTOM, 0, 0); } public void showFromTop(View anchor) { initPopupWindow(TYPE_MATCH_PARENT); mPopupWindow.setAnimationStyle(R.style.AnimationFromTop); mPopupWindow.showAtLocation(anchor, Gravity.LEFT | Gravity.TOP, 0, getStatusBarHeight()); } /** * touch outside dismiss the popupwindow, default is ture * @param isCancelable */ public void setCancelable(boolean isCancelable) { if (isCancelable) { mPopupWindow.setOutsideTouchable(true); mPopupWindow.setFocusable(true); }else { mPopupWindow.setOutsideTouchable(false); mPopupWindow.setFocusable(false); } } public void initPopupWindow(int type) { if (type == TYPE_WRAP_CONTENT) { mPopupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); }else if (type == TYPE_MATCH_PARENT) { mPopupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); } mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); setCancelable(true); } private int getStatusBarHeight() { return Math.round(25 * Resources.getSystem().getDisplayMetrics().density); }}
调用:
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private PopupWindowHelper popupWindowHelper; private View popView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button).setOnClickListener(this); popView = LayoutInflater.from(this).inflate(R.layout.popupview, null); popupWindowHelper = new PopupWindowHelper(popView); popView.findViewById(R.id.button3).setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: popupWindowHelper.showAsPopUp(v); break; default: break; } }}
代码:https://github.com/jdsjlzx/PopupWindowHelper
0 1
- Android 自定义PopupWindow指定位置或给定View坐标弹出
- Android 自定义PopupWindow指定位置或给定View坐标弹出
- PopupWindow指定位置弹出
- PopupWindow之在指定位置弹出小结
- popupWindow定时消失和指定位置弹出
- Android自定义View之popupwindow进阶封装:高仿ios “item动画弹出”效果的popupwindow。
- 自定义Popupwindow并指定显示位置
- PopupWindow弹出框(自定义弹出框)自定义位置
- Android popupwindow 弹出的位置问题
- Android PopupWindow怎么合理控制弹出位置
- Android PopupWindow怎么合理控制弹出位置
- popupWindow弹出自定义布局+弹出布局的位置控制
- Android中PopupWindow显示在指定位置
- Android中PopupWindow显示在指定位置
- Android中PopupWindow自定义坐标实现
- Android中PopupWindow自定义坐标实现
- Android中PopupWindow自定义坐标实现
- PopupWindow的弹出位置
- 电话状态
- 存储过程详解
- cryptos
- 如何复制js脚本alert或者系统对话提示框的内容
- scoket.io
- Android 自定义PopupWindow指定位置或给定View坐标弹出
- 《Neural network and deep learning》学习笔记(一)
- 欢迎使用CSDN-markdown编辑器
- MongoDB 删除文档
- 只用2GB的内存找出20亿个整数中找到出现次数最多的数
- 九度 1411 转圈
- Java开发中的23种设计模式之创建型模式
- 关于iframe 局部刷新 全部刷新 的解决办法以及嵌套 嵌套html页面
- HDU 1500 Chopsticks(DP)