开发中遇到的还挺多的,在此记录下简单用法
private PopupWindow popupWindow;private View popView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//弹出pop();}});}private void pop(){popView = View.inflate(this, R.layout.activity_pop, null);//context的方法,获取windowManagerWindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);//获取屏幕对象Display defaultDisplay = windowManager.getDefaultDisplay();//获取屏幕的宽、高,单位是像素int width = defaultDisplay.getWidth();int height = defaultDisplay.getHeight();popupWindow = new PopupWindow(popView, width, (int) (height*0.83));// 设置PopupWindow的背景,这个必须要有popupWindow.setBackgroundDrawable(new BitmapDrawable());popupWindow.setFocusable(true);// 能否响应外部点击事件popupWindow.setOutsideTouchable(true);// 弹出的键盘覆盖popwindow,不写的话键盘会把Window顶上去popupWindow.setInputMethodMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);// 添加弹出动画popupWindow.setAnimationStyle(R.style.pop_style);// 弹出位置,第一个参数写啥也行,第二个是弹出,后两个是偏移量popupWindow.showAtLocation(et_clear, Gravity.BOTTOM, 0, 0);}
pop.style
<style name="pop_style" parent="android:Animation"><item name="android:windowEnterAnimation">@anim/pop_in</item><item name="android:windowExitAnimation">@anim/pop_out</item></style>
anim/pop_in.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:duration="200"android:fromYDelta="100%p"android:toYDelta="0" /><alphaandroid:duration="200"android:fromAlpha="0.0"android:toAlpha="1.0" /></set>
anim/pop_out.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:duration="200"android:fromYDelta="0"android:toYDelta="50%p" /><alphaandroid:duration="200"android:fromAlpha="1.0"android:toAlpha="0.0" /></set>
在此说下构造函数
/*** <p>Create a new non focusable popup window which can display the* <tt>contentView</tt>. The dimension of the window must be passed to* this constructor.</p>** <p>The popup does not provide any background. This should be handled* by the content view.</p>** @param contentView the popup's content* @param width the popup's width* @param height the popup's height*/public PopupWindow(View contentView, int width, int height)
-
第一个参数,加载布局
-
后两个参数则是他要加载的大小
当使用 WindowManager.LayoutParams.
MATCH_PARENT时,不论布局写的多大,它都会充满全屏
当使用 WindowManager.LayoutParams.
WRAP_CONTENT时,如果内部的RecyclerView的高度用
WRAP_CONTENT则会被压缩到没有,所以一定要给他一个高度,或者给他的父布局一个高度