首页 > 其他分享 >PopupWindow(悬浮框)的基本使用

PopupWindow(悬浮框)的基本使用

时间:2023-03-20 22:32:37浏览次数:38  
标签:基本 PopupWindow 悬浮 int android btn public View

后一个用于显示信息的UI控件——PopupWindow(悬浮框),如果你想知道他长什么样子,你可以打开你手机的QQ,长按列表中的某项,这个时候后弹出一个黑色的小对话框,这种就是PopupWindow了,和AlertDialog对话框不同的是,他的位置可以是随意的;

另外AlertDialog是非堵塞线程的,而PopupWindow则是堵塞线程的!而官方有这样一句话来介绍PopupWindow:

A popup window that can be used to display an arbitrary view. The popup window is

a floating container that appears on top of the current activity.

1.相关方法解读

1)几个常用的构造方法

我们在文档中可以看到,提供给我们的PopupWindow的构造方法有九种之多,这里只贴实际开发中用得较多的几个构造方法:

  • public PopupWindow (Context context)
  • public PopupWindow(View contentView, int width, int height)
  • public PopupWindow(View contentView)
  • public PopupWindow(View contentView, int width, int height, boolean focusable)

参数就不用多解释了吧,contentView是PopupWindow显示的View,focusable是否显示焦点

2)常用的一些方法

下面介绍几个用得较多的一些方法,其他的可自行查阅文档:

  • setContentView(View contentView):设置PopupWindow显示的View
  • getContentView():获得PopupWindow显示的View
  • showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移
  • showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移
  • showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移PS:parent这个参数只要是activity中的view就可以了!
  • setWidth/setHeight:设置宽高,也可以在构造方法那里指定好宽高,除了可以写具体的值,还可以用WRAP_CONTENT或MATCH_PARENT,popupWindow的width和height属性直接和第一层View相对应。
  • setFocusable(true):设置焦点,PopupWindow弹出后,所有的触屏和物理按键都由PopupWindows处理。其他任何事件的响应都必须发生在PopupWindow消失之后,(home 等系统层面的事件除外)。比如这样一个PopupWindow出现的时候,按back键首先是让PopupWindow消失,第二次按才是退出activity,准确的说是想退出activity你得首先让PopupWindow消失,因为不并是任何情况下按back PopupWindow都会消失,必须在PopupWindow设置了背景的情况下 。
  • setAnimationStyle(int):设置动画效果

2.使用代码示例

运行效果图

PopupWindow(悬浮框)的基本使用_android

实现关键代码

先贴下动画文件:anim_pop.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0"
android:toAlpha="1"
android:duration="2000">
</alpha>
</set>

接着是popupWindow的布局:item_popip.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ic_pop_bg"
android:orientation="vertical">

<Button
android:id="@+id/btn_xixi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="嘻嘻"
android:textSize="18sp" />

<Button
android:id="@+id/btn_hehe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="呵呵"
android:textSize="18sp" />

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

private Button btn_show;
private Context mContext;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = MainActivity.this;
btn_show = (Button) findViewById(R.id.btn_show);
btn_show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
initPopWindow(v);
}
});
}


private void initPopWindow(View v) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_popup, null, false);
Button btn_xixi = (Button) view.findViewById(R.id.btn_xixi);
Button btn_hehe = (Button) view.findViewById(R.id.btn_hehe);
//1.构造一个PopupWindow,参数依次是加载的View,宽高
final PopupWindow popWindow = new PopupWindow(view,
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

popWindow.setAnimationStyle(R.anim.anim_pop); //设置加载动画

//这些为了点击非PopupWindow区域,PopupWindow会消失的,如果没有下面的
//代码的话,你会发现,当你把PopupWindow显示出来了,无论你按多少次后退键
//PopupWindow并不会关闭,而且退不出程序,加上下述代码可以解决这个问题
popWindow.setTouchable(true);
popWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
// 这里如果返回true的话,touch事件将被拦截
// 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss
}
});
popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000)); //要为popWindow设置一个背景才有效


//设置popupWindow显示的位置,参数依次是参照View,x轴的偏移量,y轴的偏移量
popWindow.showAsDropDown(v, 50, 0);

//设置popupWindow里的按钮的事件
btn_xixi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "你点击了嘻嘻~", Toast.LENGTH_SHORT).show();
}
});
btn_hehe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "你点击了呵呵~", Toast.LENGTH_SHORT).show();
popWindow.dismiss();
}
});
}
}

标签:基本,PopupWindow,悬浮,int,android,btn,public,View
From: https://blog.51cto.com/u_15641375/6138659

相关文章

  • Redis基本架构
    Redis基本架构为###存储模块,保存数据,数据模型为key-value形式,value支持丰富的数据类型。包括字符串,列表,哈希表,集合等。不同的数据类型能支持不同的业务需求###操作模块,......
  • 【微信网页授权】SpringBoot+uniapp实现网页授权获取用户基本信息
    前言......
  • 3.19 基本Dos命令
    常用的Dos命令1.盘符切换:(英文状态下)例C盘换D盘(直接在后边加所要换盘的名称和冒号)  2查看当前目录下的所有文件名    :   直接加Dir3切换目录(用cd......
  • minio基本使用
    使用例子packagecom.lomi.minio;importcn.hutool.core.io.FileUtil;importcn.hutool.core.io.IoUtil;importcn.hutool.http.ContentType;importcn.hutool.jso......
  • 性能测试基本知识
    一、软件项目中性能测试的概念: 性能测试是指通过特定方式,对被测系统按照一定策略事假压力,获取系统响应时间、TPS(TransactionPerSencond)、吞吐量、资源利用率等性能指标,......
  • 关于element-ui修改.el-submenu__title悬浮样式不生效的问题
    以下内容仅供个人学习使用据网友分析原因:二级菜单是使用slot添加的,单独给这个slot添加样式,等渲染出来,样式没有了,需要有深选择器。可以使用深选择器,加入以下样式方可解决......
  • 爬虫:bs4介绍,遍历文档树、bs4搜索文档树、css选择器、selenium基本使用、无界面浏览器
    目录一、bs4介绍,遍历文档树1.1bs4模块介绍1.2bs4的遍历文档树二、bs4搜索文档树1.2find_all(name,attrs,recursive,text,**kwargs)2.2find(name,attrs,r......
  • Docker:docker基本概念
    前言docker必须安装在linux操作系统docker基本概念镜像(image)镜像是一整套【应用+环境】的集合。可以类比为Java的类容器(container)容器是镜像的实例。可以类比为Java......
  • 6.基本命令
    查看pod: kubectlgetpod 查看你默认命名空间的pod kubectlgetpod-n空间名 查看指定命名空间下的pod kubectlgetpod-owide-A 查看所有......
  • [数据结构]循环链表及其基本操作
    /**@Author:*@data:2019-12-0319:47:29*@LastModifiedby:*@LastModifiedtime:2019-12-0320:00:06*/#include<iostream>#include<cstdio>usingnamesp......