效果图
第一种(图一)基本使用
分装一个设置的类 WrapPopupWindow
package com.zhh.android.demo.tools;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
/**
* Created by 16838 on 2019/1/5.
*/
public class WrapPopupWindow {
// 设置布局
public static View createView(Context context,int resource){
View view = LayoutInflater.from(context).inflate(resource, null, false);
return view;
}
// 设置popupWindow
public static PopupWindow setPopupWindow(View view){
//1.构造一个PopupWindow,参数依次是加载的View,宽高
final PopupWindow popupWindow = new PopupWindow(view,
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
//设置加载动画
// popupWindow.setAnimationStyle(R.style.AnimBottom);
//这些为了点击非PopupWindow区域,PopupWindow会消失的,如果没有下面的
//代码的话,你会发现,当你把PopupWindow显示出来了,无论你按多少次后退键
//PopupWindow并不会关闭,而且退不出程序,加上下述代码可以解决这个问题
popupWindow.setTouchable(true);
popupWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
// 这里如果返回true的话,touch事件将被拦截
// 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss
// 拦截后,泡泡窗口子控件的点击事件等,失效
}
});
popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000)); //要为popWindow设置一个背景才有效
return popupWindow;
}
}
Activity中调用
private void setPopopWindow(){
// 设置布局
View view = WrapPopupWindow.createView(context, R.layout.item_popup_wrap);
// 拿到设置后的 PopupWindow 对象
final PopupWindow popupWindow = WrapPopupWindow.setPopupWindow(view);
// 展示
popupWindow.showAsDropDown(buttonShow,50,0);
Button btn_xixi = (Button) view.findViewById(R.id.btn_xixi);
Button btn_hehe = (Button) view.findViewById(R.id.btn_hehe);
btn_xixi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
Toast.makeText(context, "嘻嘻", Toast.LENGTH_SHORT).show();
}
});
btn_hehe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
Toast.makeText(context, "呵呵", Toast.LENGTH_SHORT).show();
}
});
}
PopupWindow的布局文件 item_popup_wrap.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/ic_pop_bg"
>
<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>
ok,讲完了
第二种(图二)底部滑入
封装一个设置的类 MatchPopupWindow
package com.zhh.android.demo.tools;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widge