Android学习之PopupWindow使用

一、相关函数介绍
1.构造函数(一共九个):

//1
 public PopupWindow(Context context) {
        this(context, null);
    }
//2
 public PopupWindow(Context context, AttributeSet attrs) {
      this(context, attrs, com.android.internal.R.attr.popupWindowStyle);
    }
//3
 public PopupWindow(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }
//4
 public PopupWindow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {}
//5
 public PopupWindow() {
        this(null, 0, 0);
    }
//6   
public PopupWindow(View contentView) {
        this(contentView, 0, 0);
    }
//7   
public PopupWindow(int width, int height) {
        this(null, width, height);
    }
//8
 public PopupWindow(View contentView, int width, int height) {
        this(contentView, width, height, false);
    }
//9   
public PopupWindow(View contentView, int width, int height, boolean focusable) {}

其中比较常用最后两个,其中参数:
contentView:PopuoWindow的弹窗布局
width:PopuoWindow宽度
height:PopuoWindow高度
focusable:是否能获得焦点

2.初始化PopuoWindow的View

public void setContentView(View contentView)

例如:

PopupWindow popupWindow = new PopupWindow(context);
popupWindow.setContentView(contentview);

完整的构造构造代码如下:

View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null);
PopupWindwo popupWindow = PopupWindow (context);
popupWindow .setContentView(contentView);
popupWindow .setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow .setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

3.显示PopupWindow
popupWindow的显示函数如下:

//相对某个控件的位置(正左下方),无偏移
showAsDropDown(View anchor)//相对某个控件的位置,有偏移;xoff表示x轴的偏移,正值表示向左,负值表示向右;yoff表示相对y轴的偏移,正值是向下,负值是向上;
showAsDropDown(View anchor, int xoff, int yoff)//相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
showAtLocation(View parent, int gravity, int x, int y)

接下来我们一个一个看,先看showAtLocation好了
3.1 showAtLocation
首先其函数原型是:

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

先来一波参数解析:
参数1——view:

这个view的作用是用来获取当前窗口的,
popupWindow通过这个参数获得当前窗口的令牌,从而确定所显示的窗口,实际显示的问题与该参数无关。重点在最后一句,实际显示的问题与该参数无关,也就是它对窗口的显示位置没有影响

参数2——int参数:

这个参数是指popupwindow中心点的位置,以当前屏幕为基准,默认居中(Gravity.CENTER)。
其他参数: Gravity.TOP、Gravity.LEFT、Gravity.BOTTOM、Gravity.RIGHT

参数3、4——x,y:

这两个参数分别对应相对于中心点位置的水平偏移量以及相对于中心点位置的垂直偏移量

那么,先看下效果:

mPpw.showAtLocation(btnShow, Gravity.CENTER,0,0);

在这里插入图片描述

mPpw.showAtLocation(btnShow, Gravity.TOP,0,0);

在这里插入图片描述

可以看到我们偏移量的设置都为0,但弹窗并没有贴在我们传入的view即显示弹窗按钮上面,而是在屏幕最上方,所以也可以看出弹窗位置的确定与我们传入的view没什么关系。

3.2
showAsDropDown

首先,这个函数表示相对于某个控件的位置,这时候就与上面的showAtLocation不同了,弹窗位置的确定与我们传入的view是有直接关系的,剩下两个参数x,y则代表水平和垂直方向的偏移。
注意:弹窗位置默认是view的左下角
看下效果:

mPpw.showAsDropDown(btnShow,0,0);

在这里插入图片描述
4.其他控制函数

//关闭PopupWindow
public void dismiss()
//设置PopupWindow获得焦点,获得焦点后可以交互,如果弹窗里面有Editor,别忘记设置这个为true
public void setFocusable(boolean focusable)
//设置PopupWindow是否可以触摸交互
public void setTouchable(boolean touchable)
//设置点击PopupWindow外的区域是否关闭
public void setOutsideTouchable(boolean touchable)
//设置了背景
public void setBackgroundDrawable(Drawable background)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值