这里自己最初写的时候有一个小问题,是由于自己没有仔细阅读popwindow源码导致的。
问题:自定义popwindow设置了Gravity.Bottom,从底部弹出,可是popwindow却总是和底部有大概6dp的间隙。
开始时没有注意,后来才发现原来Popwindow自身会带有一个背景色,会向下延伸出来一定的虚影效果,这里是需要设置的,我之前设置的是透明色, 所以就自然有了间隙,后来我把背景色改成和布局背景色一样的颜色,这个问题就迎刃而解了。
这里附上自己Popwindow的源码:
public class BottomPopWindow extends PopupWindow {
private String TAG = BottomPopWindow.class.getSimpleName();
private View mMenuView;
private popOnClick listener;
public BottomPopWindow(Context context,boolean enableOutsideTouchDisMiss) {
super(context);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//自定义布局
mMenuView = inflater.inflate(R.layout.popwindow_bottom, null);
//设置PopupWindow的View
this.setContentView(mMenuView);
//设置SelectPicPopupWindow弹出窗体的宽
this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
//设置SelectPicPopupWindow弹出窗体的高
this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
//设置SelectPicPopupWindow弹出窗体可点击
this.setFocusable(true);
//设置SelectPicPopupWindow弹出窗体动画效果
this.setAnimationStyle(R.style.popupAnimation);
//实例化一个ColorDrawable颜色为半透明
//ColorDrawable dw = new ColorDrawable(0xb0000000);
//设置SelectPicPopupWindow弹出窗体的背景
//this.setBackgroundDrawable(dw);
// this.getBackground().setAlpha(0);
//popwindow本身有一个背景色,距离底部会有一定距离,这里给设置了background就会被遮盖住
setBackgroundDrawable(new ColorDrawable(Color.WHITE));
final Activity activity = (Activity) context;
WindowManager.LayoutParams params = activity.getWindow().getAttributes();
params.alpha = 0.7f;
activity.getWindow().setAttributes(params);
PopwindowBottomBinding binding = DataBindingUtil.bind(mMenuView);
binding.setClick(this);
this.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams params = activity.getWindow().getAttributes();
params.alpha = 1f;
activity.getWindow().setAttributes(params);
if (listener != null) {
listener.dismiss();
}
}
});
if(!enableOutsideTouchDisMiss){
//注意这三个属性必须同时设置,不然不能disMiss,以下三行代码在Android 4.4 上是可以,然后在Android 6.0以上,下面的三行代码就不起作用了,就得用下面的方法
this.setFocusable(true);
this.setOutsideTouchable(false);
this.setBackgroundDrawable(null);
//注意下面这三个是contentView 不是PopupWindow
this.getContentView().setFocusable(true);
this.getContentView().setFocusableInTouchMode(true);
this.getContentView().setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
dismiss();
return true;
}
return false;
}
});
//在Android 6.0以上 ,只能通过拦截事件来解决
this.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int x = (int) event.getX();
final int y = (int) event.getY();
int mWidth = getContentView().getMeasuredWidth();
int mHeight = getContentView().getMeasuredHeight();
if ((event.getAction() == MotionEvent.ACTION_DOWN)
&& ((x < 0) || (x >= mWidth) || (y < 0) || (y >= mHeight))) {
Log.e(TAG,"out side ");
return true;
} else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
Log.e(TAG,"out side ...");
return true;
}
return false;
}
});
}else{
this.setFocusable(true);
this.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
this.setOutsideTouchable(true);
}
// update
this.update();
}
public void delete(View v) {
if (listener != null) {
listener.delete();
}
}
public void revise(View v) {
if (listener != null) {
listener.revise();
}
}
public View getView() {
return mMenuView;
}
public void setPopOnClick(popOnClick listener) {
this.listener = listener;
}
public interface popOnClick {
void delete();
void revise();
void dismiss();
}
}
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="click"
type="com.hivescm.wms.rf.widget.BottomPopWindow"></variable>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background_light"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp">
<Button
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
android:background="@drawable/bg_style_edittext"
android:onClick="@{click::delete}"
android:text="删除" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.1"></View>
<Button
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
android:background="@drawable/bg_style_edittext"
android:onClick="@{click::revise}"
android:text="修改" />
</LinearLayout>
</layout>
还以一个问题,popwindow点击外部不消失的实现,这里尝试了很多方法都没有实现,最后找到一位大神的源码,对自己的代码进行了简单的改造。
这里贴上它的博客原文链接,大家对Popwindow点击外部不消失有问题的可以看一下。
大神博客链接:http://www.cnblogs.com/gongxiaojiu/p/7101255.html