PopupWindow为什么要设置setBackgroundDrawable(new BitmapDrawable());

tyj312699828
2012-07-13
知道答主
回答量:2
采纳率:0%
帮助的人:3262
展开全部
这个可以控制popupwindow的隐藏,可以实现点击popupwindow以外的区域时隐藏popupwindow,还有一个功能就是响应back键。实现按back键可以隐藏popupwindow的功能。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
hanbing99wt
2013-01-07
知道答主
回答量:24
采纳率:0%
帮助的人:7.5万
展开全部
设置后点击旁边区域可以关闭PopupWindow
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
xlc758141116
2013-04-15
知道答主
回答量:4
采纳率:0%
帮助的人:7.9万
展开全部
点击空白处 PopupWindow 消失 的作用
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
奉赖锦9066
2013-08-30 · TA获得超过156个赞
知道小有建树答主
回答量:191
采纳率:0%
帮助的人:83万
展开全部
源码中有下面一段:
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);
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
失语儿童
2012-05-22
知道答主
回答量:2
采纳率:0%
帮助的人:5.6万
展开全部
哈哈,今天也遇到了这个问题。
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 2条折叠回答
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式