【每天积累一点点】将PopupWindow显示在指定位置

本文介绍如何在Android中实现PopupWindow的精确位置显示,特别是在控件tvMl的右下角显示PopupWindow的方法。通过LayoutInflater加载布局,并使用PopupWindow的showAtLocation方法结合Gravity属性进行定位。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//以想在控件tvMl的右下角显示PopupWindow为例(popupWindow的右上角在tvMl的右下角)
            View popPhotoView = LayoutInflater.from(this).inflate(R.layout.popup_ml_choose, null);
            mlWindow = new PopupWindow(popPhotoView, Utils.dip2px(this, 132), Utils.dip2px(this, 176), true);
            mlWindow.setBackgroundDrawable(new BitmapDrawable());
            mlWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
            mlWindow.setOutsideTouchable(true);
            //获取tvMl在屏幕中的位置,其中location[0]是tvMl左上角的x位置,location[1]是tvMl左上角y轴的位置
            int[] location = new int[2];
            tvMl.getLocationOnScreen(location);
//            showAtLocation(View parent, int gravity, int x, int y),其中x,y参数是PopupWindow的左上角的(x,y)坐标
            mlWindow.showAtLocation(tvMl, Gravity.NO_GRAVITY, location[0] + tvMl.getWidth() - mlWindow.getWidth(), location[1] + tvMl.getHeight());

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值