1、介绍
(1)使用PopupWindow可实现弹出窗口效果,,其实和AlertDialog一样,也是一种对话框,两者也经常混用,但是也各有特点。
AlertDialog是非阻塞式对话框:AlertDialog弹出时,后台还可以做事情;而PopupWindow是阻塞式对话框:PopupWindow弹出时,程序会等待。
(2)PopupWindow可以指定位置,并且弹出的框位置在指定位置的时可以用。当在背景色变色,在屏幕中间显示时并且不能操作其它内容时使用AlertDialog。
2、PopupWindow属性
(1)定义PopupWindow有如下四重方法,其中contentView(自己定义的弹出框布局),width,height三个属性必须设置,所以建议用方法四
//方法一:
public PopupWindow (Context context)
//方法二:
public PopupWindow(View contentView)
//方法三:
public PopupWindow(View contentView, int width, int height)
//方法四:
public PopupWindow(View contentView, int width, int height, boolean focusable)
(2)显示弹出框位置
//相对某个控件的位置(正左下方),无偏移
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):
(3)其它函数
//关闭弹出框
public void dismiss()
//弹出框是否具有获取焦点的能力,默认为false,就是如果弹出框中有Edittext时,并且想获取焦点,则需要设置为true
public void setFocusable(boolean focusable)
//是否相应touch事件,默认为true,如果设置为false,则弹出框的所有点击按钮都不可点击
public void setTouchable(boolean touchable)
//PopupWindow以外的区域是否可点击,即如果点击PopupWindow以外的区域,PopupWindow是否会消失。需要加入背景色才好使
public void setOutsideTouchable(boolean touchable)
//设置背景色的,只有设置了背景色,返回按钮才会取消PopupWindow显示,不然会返回上一个Activity
public void setBackgroundDrawable(Drawable background))
3、仿微信实例
(1)效果图
(2)代码首先需要写一个xml文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/btn1"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center_vertical"
android:background="#393a3f"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:src="@drawable/icon_edit_delete"
android:layout_marginLeft="15dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:text="发起群聊"
android:textColor="#ffffff"
android:layout_marginLeft="15dp"
android:textSize="15sp"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:background="@color/gray_font"
android:layout_height="1dp" />
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center_vertical"
android:background="#393a3f"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:src="@drawable/icon_edit_delete"
android:layout_marginLeft="15dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:text="添加朋友"
android:textColor="#ffffff"
android:layout_marginLeft="15dp"
android:textSize="15sp"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:background="@color/gray_font"
android:layout_height="1dp" />
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center_vertical"
android:background="#393a3f"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:src="@drawable/icon_edit_delete"
android:layout_marginLeft="15dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:text="扫一扫"
android:textColor="#ffffff"
android:layout_marginLeft="15dp"
android:textSize="15sp"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:background="@color/gray_font"
android:layout_height="1dp" />
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center_vertical"
android:background="#393a3f"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:src="@drawable/icon_edit_delete"
android:layout_marginLeft="15dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:text="收复款"
android:textColor="#ffffff"
android:layout_marginLeft="15dp"
android:textSize="15sp"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:background="@color/gray_font"
android:layout_height="1dp" />
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center_vertical"
android:background="#393a3f"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:src="@drawable/icon_edit_delete"
android:layout_marginLeft="15dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:text="帮助与反馈"
android:textColor="#ffffff"
android:layout_marginLeft="15dp"
android:textSize="15sp"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:background="@color/gray_font"
android:layout_height="1dp" />
</LinearLayout>
(3)activity代码
//显示popupWindow
private fun showPopupWindow() {
//获取自定义菜单的布局文件
val popupWindow_view = layoutInflater.inflate(R.layout.add_rigth_layout, null, false)
//创建popupWindow,设置宽度和高度
popupWindow = PopupWindow(popupWindow_view, 200, 300, true)
//内部控件的点击事件
btn1!!.setOnClickListener(this)
//设置菜单的显示位置
popupWindow!!.showAsDropDown(right_btn, -150, 20)
//兼容5.0点击其他位置隐藏popupWindow
popupWindow_view.setOnTouchListener(View.OnTouchListener { view, motionEvent ->
//必须写 v.performClick();解决与单击事件的冲突
when (motionEvent.action) {
MotionEvent.ACTION_DOWN ->
//如果菜单不为空,且菜单正在显示
if (popupWindow!!.isShowing()) {
popupWindow!!.dismiss()//隐藏菜单
popupWindow = null//初始化菜单
}
MotionEvent.ACTION_UP -> view.performClick()
else -> {
}
}
false
})
}
(4)文章参考了:https://blog.csdn.net/harvic880925/article/details/49272285的内容,感觉他写的更细一点,也有进入和退出动画。