PopupWindow弹出窗体

显示方法 显示位置
showAsDropDown(View anchor, int xoff, int yoff) 显示在anchor控件的下方
showAtLocation(View parent, int gravity, int x, int y) 显示在parent控件的某个位置

1、popupwindow和对话框的区别

PopupWindow弹出窗体可以在任意位置弹出窗体,而对话框只能出现屏幕最中间。

2、如何创建自定义窗体

1、构造方法:public PopupWindow (Context context):context是一个上下文对象
2、必须设置的3大要素:
     setContentView():设置布局
     setWidth():设置宽度
     setHeight():设置高度
3、显示窗体:
   a.显示在某个指定控件的下方
     showAsDropDown(View anchor):
     showAsDropDown(View anchor, int xoff, int yoff);//xoff和yoff都是偏移量
   b.指定父视图,显示在父控件的某个位置(Gravity.TOP,Gravity.RIGHT等)
     showAtLocation(View parent, int gravity, int x, int y);
     //gravity可以是Gravity.TOP、Gravity.BOTTOM、Gravity.LEFT、Gravity.RIGHT

3、效果

在这里插入图片描述

a、具体操作和代码

步骤1.实例化PopupWindow对象
步骤2.设置自定义布局、宽度和高度
步骤3.指定位置显示: showAsDropDown() showAtLocation()
PopupWindow popupWindow = new PopupWindow(MainActivity.this);
                popupWindow.setWidth(300);//设置窗体的宽
                popupWindow.setHeight(200);//设置窗体的高
                popupWindow.setOutsideTouchable(true);//设置点击其他地方,窗体消失
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout, null);//找到一个布局文件
                popupWindow.setContentView(view);//将布局设置上去
                /**
		         * @param parent 父布局
		         * @param gravity gravity可以是Gravity.TOP、Gravity.BOTTOM、Gravity.LEFT、Gravity.RIGHT。。。。
		         * @param x x轴偏移量
		         * @param y y轴偏移量
		         */
                popupWindow.showAtLocation(view1, Gravity.BOTTOM,0,0);//设置在窗体的下方显示
                //popupWindow.showAsDropDown(imageView);  //在对应控件的下方显示

设置其透明度

WindowManager.LayoutParams attributes = getWindow().getAttributes();//获取窗体中的属性
                attributes.alpha=0.5f;//将其背景设置半透明
                getWindow().setAttributes(attributes);//修改完毕后,重新将属性设置回去

窗体消失事件

 popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
    
    
            @Override
            public void onDismiss() {
    
    
                WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
                layoutParams.alpha=1f;
                getWindow().setAttributes(layoutParams);
            }
        });

3、设置动画

创建一个anim文件夹,在里面写动画
在这里插入图片描述

在这里插入图片描述

在stylexml中设置style
在这里插入图片描述

自定义popupwindoe

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);


        /**
         * 设置背景只有设置了这个才可以点击外边和BACK消失
         */
        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;
            }
        });


        /**
         * 初始化View与监听器
         */
        initView();

        initListener();
    }




    private void initView() {
    
    

    }

    private void initListener() {
    
    

    }


}

猜你喜欢

转载自blog.csdn.net/weixin_43841463/article/details/91357368
今日推荐