PopupWindow基于某控件的显示

sendmessage_linearlayout为目标控件

popupWindow:自定义的popupWindow

控件之上:

popupWindow.showAtLocation(sendmessage_linearlayout, Gravity.NO_GRAVITY

                        ,location[0]
                        ,location[1] - popupWindow.getHeight());

控件之下:

popupWindow.showAtLocation(sendmessage_linearlayout, Gravity.NO_GRAVITY

                        ,location[0]
                        ,location[1] +popupWindow.getHeight());

控件之左:

popupWindow.showAtLocation(sendmessage_linearlayout, Gravity.NO_GRAVITY

                        ,location[0] - popupWindow.getWidth()/2
                        ,location[1] );

控件之右:

popupWindow.showAtLocation(sendmessage_linearlayout, Gravity.NO_GRAVITY

                        ,location[0] + popupWindow.getWidth()/2
                        ,location[1] );

中上:


popupWindow.showAtLocation(sendmessage_linearlayout, Gravity.NO_GRAVITY

                        ,location[0] + popupWindow.getWidth()/2
                        ,location[1] - popupWindow.getHeight());
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要让PopupWindow的高度占满控件的下方,可以通过以下步骤实现: 1. 获取控件的位置信息,包括控件在屏幕上的位置和控件的宽度。 2. 创建一个PopupWindow对象并设置其宽度为控件的宽度,高度设置为WRAP_CONTENT。 3. 设置PopupWindow的内容视图。 4. 计算PopupWindow应该显示的位置,使其完全显示在屏幕上。 5. 设置PopupWindow的位置并显示出来。 下面是示例代码: ``` // 获取控件位置信息 int[] location = new int[2]; view.getLocationOnScreen(location); int x = location[0]; int y = location[1]; int width = view.getWidth(); // 创建PopupWindow对象 PopupWindow popupWindow = new PopupWindow(context); popupWindow.setWidth(width); popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); // 设置PopupWindow的内容视图 View contentView = LayoutInflater.from(context).inflate(R.layout.popup_layout, null); popupWindow.setContentView(contentView); // 计算PopupWindow应该显示的位置 int screenHeight = context.getResources().getDisplayMetrics().heightPixels; int popupHeight = contentView.getMeasuredHeight(); int margin = context.getResources().getDimensionPixelSize(R.dimen.margin); int popupY = y + view.getHeight() + margin; if (popupY + popupHeight > screenHeight) { popupY = y - popupHeight - margin; } // 设置PopupWindow的位置并显示出来 popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, x, popupY); ``` 在示例代码中,我们首先获取了控件的位置信息,然后创建了一个PopupWindow对象并设置其宽度为控件的宽度,高度设置为WRAP_CONTENT。接着,我们设置了PopupWindow的内容视图,并计算了PopupWindow应该显示的位置,以确保它完全显示在屏幕上。最后,我们设置PopupWindow的位置并调用showAtLocation方法将其显示出来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值