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)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PopupWindow是一种可以在当前界面上方显示的弹出窗口,通常用于显示一些额外的信息或者提供用户操作的选项。在Android中,可以使用PopupWindow类来创建弹出窗口。 以下是使用PopupWindow的一般步骤: 1. 创建PopupWindow对象:使用PopupWindow的构造函数创建一个PopupWindow对象。 2. 设置PopupWindow的属性:设置PopupWindow的大小、位置、背景等属性。 3. 设置PopupWindow的内容视图:使用setContentView方法设置PopupWindow的内容视图,这可以是一个布局文件或者一个View对象。 4. 显示PopupWindow使用showAsDropDown、showAtLocation等方法显示PopupWindow。 5. 处理PopupWindow的事件:设置PopupWindow的监听器,处理PopupWindow的各种事件。 以下是一个简单的例子,展示如何使用PopupWindow: ``` // 创建PopupWindow对象 PopupWindow popupWindow = new PopupWindow(context); // 设置PopupWindow的属性 popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT); popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); popupWindow.setFocusable(true); // 设置PopupWindow的内容视图 View contentView = LayoutInflater.from(context).inflate(R.layout.popup_layout, null); popupWindow.setContentView(contentView); // 显示PopupWindow popupWindow.showAsDropDown(anchorView); // 处理PopupWindow的事件 contentView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 处理点击事件 popupWindow.dismiss(); } }); ``` 在上面的代码中,我们创建了一个PopupWindow对象,并设置了宽高、背景等属性。然后,我们使用LayoutInflater加载了一个布局文件作为PopupWindow的内容视图,并使用setContentView方法设置了PopupWindow的内容视图。最后,我们使用showAsDropDown方法显示了PopupWindow,并设置了一个点击事件处理器来处理用户点击PopupWindow的事件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值