先来看看效果图:
一、布局
2、自定义MypopupWindow继承PopupWindow
public class MyPopupWindow extends PopupWindow {
3、重写构造方法与动画样式
在styles.xml自定义样式,动画
- @anim/pop_in
- @anim/pop_out
pop_in
pop_out
4、重写构造方法并设置点击外部可以消失监听
super(context);
this.mContext=context;
//打气筒
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//打气
mContentView = mInflater.inflate(R.layout.layout_dialog,null);
//设置View
setContentView(mContentView);
//设置宽与高
setWidth(WindowManager.LayoutParams.MATCH_PARENT);
setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
setAnimationStyle(R.style.MyPopupWindow);
setBackgroundDrawable(new ColorDrawable());
setFocusable(true);
setOutsideTouchable(true);
setTouchable(true);
setTouchInterceptor(new View.onTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
return true;
}
//不是点击外部
return false;
}
});
5、显示及设置窗口变暗与变亮
public void displayDialog(View view){
MyPopupWindow myPopupWindow = new MyPopupWindow(this);
myPopupWindow.showAsDropDown(mBtnDispaly,0,0);
lightOff();
myPopupWindow.setonDismissListener(new PopupWindow.onDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.alpha=1.0f;
getWindow().setAttributes(layoutParams);
}
});
}
private void lightOff() {
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.alpha=0.3f;
getWindow().setAttributes(layoutParams);
}
6、完整
package liu.basedemo.view;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import liu.basedemo.R;
public class MyPopupWindow extends PopupWindow {
Context mContext;
private LayoutInflater mInflater;
private View mContentView;
public MyPopupWindow(Context context) {
super(context);
this.mContext=context;
//打气筒
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//打气
mContentView = mInflater.inflate(R.layout.layout_dialog,null);
//设置View
setContentView(mContentView);
//设置宽与高
setWidth(WindowManager.LayoutParams.MATCH_PARENT);
setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
setAnimationStyle(R.style.MyPopupWindow);
setBackgroundDrawable(new ColorDrawable());
setFocusable(true);
setOutsideTouchable(true);
setTouchable(true);
setTouchInterceptor(new View.onTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
return true;
}
//不是点击外部
return false;
}
});
initView();
initListener();
}
private void initView() {
}
private void initListener() {
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



