PopupWindow为啥要设置setBackgroundDrawable(new BitmapDrawable());

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PopupWindow为啥要设置setBackgroundDrawable(new BitmapDrawable());相关的知识,希望对你有一定的参考价值。

参考技术A 这个可以控制popupwindow的隐藏,可以实现点击popupwindow以外的区域时隐藏popupwindow,还有一个功能就是响应back键。实现按back键可以隐藏popupwindow的功能。 参考技术B 设置后点击旁边区域可以关闭PopupWindow 参考技术C 点击空白处 PopupWindow 消失 的作用 参考技术D 源码中有下面一段:
if (mBackground != null)
final ViewGroup.LayoutParams layoutParams = mContentView.getLayoutParams();
int height = ViewGroup.LayoutParams.MATCH_PARENT;
if (layoutParams != null &&
layoutParams.height == ViewGroup.LayoutParams.WRAP_CONTENT)
height = ViewGroup.LayoutParams.WRAP_CONTENT;


// when a background is available, we embed the content view
// within another view that owns the background drawable
PopupViewContainer popupViewContainer = new PopupViewContainer(mContext);
PopupViewContainer.LayoutParams listParams = new PopupViewContainer.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, height
);
popupViewContainer.setBackgroundDrawable(mBackground);
popupViewContainer.addView(mContentView, listParams);

mPopupView = popupViewContainer;


当mBackground不为空时设置PopupViewContainer,在PopupViewContainer中处理了点击事件,
private class PopupViewContainer extends FrameLayout
private static final String TAG = "PopupWindow.PopupViewContainer";

public PopupViewContainer(Context context)
super(context);


@Override
protected int[] onCreateDrawableState(int extraSpace)
if (mAboveAnchor)
// 1 more needed for the above anchor state
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
View.mergeDrawableStates(drawableState, ABOVE_ANCHOR_STATE_SET);
return drawableState;
else
return super.onCreateDrawableState(extraSpace);



@Override
public boolean dispatchKeyEvent(KeyEvent event)
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
if (getKeyDispatcherState() == null)
return super.dispatchKeyEvent(event);


if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getRepeatCount() == 0)
KeyEvent.DispatcherState state = getKeyDispatcherState();
if (state != null)
state.startTracking(event, this);

return true;
else if (event.getAction() == KeyEvent.ACTION_UP)
KeyEvent.DispatcherState state = getKeyDispatcherState();
if (state != null && state.isTracking(event) && !event.isCanceled())
dismiss();
return true;


return super.dispatchKeyEvent(event);
else
return super.dispatchKeyEvent(event);

第5个回答  2012-05-22 哈哈,今天也遇到了这个问题。本回答被提问者采纳

以上是关于PopupWindow为啥要设置setBackgroundDrawable(new BitmapDrawable());的主要内容,如果未能解决你的问题,请参考以下文章

popupwindow设置了消失动画,消失动画结束后有出现了。。。要怎么解决。。

从源码剖析PopupWindow 兼容Android 6.0以上版本点击外部不消失

android 在PopupWindow里面如何切换外部的Activity?

PopupWindow上面的虚拟键盘

Android中Popupwindow和Dialog的区别

Android基础控件——PopupWindow模仿ios底部弹窗