本着因为项目中需要做半透明的引导层, git上也有相应的TourGuide开源项目,但是功能需求不符。为了定制所需要的界面所以我想到了用PopupWindow弹窗 半透明来实现了界面的引导层。
LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = mInflater.inflate(R.layout.layout_guild_worker,null);
// view.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_ins));
popupWindow=newPopupWindow(view, LinearLayoutCompat.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,true);
//在PopupWindow里面就加上下面代码,让键盘弹出时,不会挡住pop窗口。
popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
popupWindow.setFocusable(true);
ColorDrawable dw =newColorDrawable(0x00000000);
popupWindow.setBackgroundDrawable(dw);
backgroundAlpha(0.5f);
popupWindow.showAtLocation(findViewById(R.id.layout), Gravity.BOTTOM
,0,0);
popupWindow.setOnDismissListener(newPopupWindow.OnDismissListener() {
@Override
public voidonDismiss() {
backgroundAlpha(1f);
}
});
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (popupWindow.isShowing()) {
popupWindow.dismiss();
}
}
});
很快问题就来了,debug运行后,一打开界面就在
popupWindow.showAtLocation(findViewById(R.id.layout), Gravity.BOTTOM,0,0);
处报错误了,
百度这个问题,说的都是什么上下文生命周期的问题, 并不适用我们今天碰到的问题
解决:
findViewById(R.id.layout).post(newRunnable() {
public voidrun() {
popupWindow.showAtLocation(findViewById(R.id.layout), Gravity.BOTTOM
,0,0);
}
});
顺便粘出设置背景透明度backgroundAlpha()代码:
public voidbackgroundAlpha(floatbgAlpha) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha= bgAlpha;//0.0-1.0
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
getWindow().setAttributes(lp);
}
网友评论