美文网首页
Android popupWindow的使用

Android popupWindow的使用

作者: 因为我的心 | 来源:发表于2020-05-20 17:13 被阅读0次

    一、前言:

    1、PopupWindow与AlertDialog的区别

    最关键的区别是AlertDialog不能指定显示位置,只能默认显示在屏幕最中间(当然也可以通过设置WindowManager参数来改变位置)。而PopupWindow是可以指定显示位置的,随便哪个位置都可以,更加灵活。
    gitHub地址:
    效果图如下:https://gitee.com/luoyanyong/PopupWindowDemo

    左侧弹出.png 右侧弹出.png 下方弹出.png 上方弹出.png

    2、PopupWindow的相关函数

    (1)、构造函数:
    //方法一:
    public PopupWindow (Context context)
    //方法二:
    public PopupWindow(View contentView)
    //方法三:
    public PopupWindow(View contentView, int width, int height)
    //方法四:
    public PopupWindow(View contentView, int width, int height, boolean focusable)
    

    首要注意:看这里有四个构造函数,但要生成一个PopupWindow最基本的三个条件是一定要设置的:View contentView,int width, int height ;少任意一个就不可能弹出来PopupWindow!!!!

    所以,如果使用方法一来构造PopupWindow,那完整的构造代码应该是这样的:

    View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null);
    PopupWindwo popWnd = PopupWindow (context);
    popWnd.setContentView(contentView);
    popWnd.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
    popWnd.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
    

    有关为什么一定要设置width和height的原因,我们后面会讲,这里说一下为什么样强制设置contentView;很简单的原因是因为PopupWindow没有默认布局,它不会像AlertDialog那样只setTitle,就能弹出来一个框。PopupWindow是没有默认布局的,它的布局只有通过我们自己设置才行。由于方法三中,含有了这三个必备条件,不用单独设置contentview或者width、height,所以构造方法三是用的最多的一个构造方法。
    最后,方法四中的focusable变量不是必须的,有关它的方法和意义,我们会在下一篇中细讲。

    (2)显示函数

    显示函数主要使用下面三个:

    //相对某个控件的位置(正左下方),无偏移
    showAsDropDown(View anchor):
    //相对某个控件的位置,有偏移;xoff表示x轴的偏移,正值表示向左,负值表示向右;yoff表示相对y轴的偏移,正值是向下,负值是向上;
    showAsDropDown(View anchor, int xoff, int yoff):
    //相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
    showAtLocation(View parent, int gravity, int x, int y):
    

    这里有两种显示方式:
    1、显示在某个指定控件的下方
    showAsDropDown(View anchor):
    showAsDropDown(View anchor, int xoff, int yoff);
    2、指定父视图,显示在父控件的某个位置(Gravity.TOP,Gravity.RIGHT等)
    showAtLocation(View parent, int gravity, int x, int y);

    (3)、其它函数
        //关闭弹窗
        public void dismiss()
        //设置PopupWindow可聚焦
        public void setFocusable(boolean focusable)
        //设置PopupWindow可触摸
        public void setTouchable(boolean touchable)
        //设置外部点击退出
        public void setOutsideTouchable(boolean touchable)
       //设置背景
        public void setBackgroundDrawable(Drawable background)
    

    这几个函数里,这篇只会用到dismiss(),用于不需要的时候,将窗体隐藏掉。

    二、使用:

    下方是自定义一下popupWindow,使用起来更加方便。

    1. MainActivity.java:

    /**
     * 仿IOS弹窗
     */
    public class MainActivity extends AppCompatActivity {
        private Button btn1;
        private Button btn2;
        private Button btn3;
        private Button btn4;
        private Button btn5;
        private LinearLayout content;
    
    
        /**
         * 弹窗左侧
         */
        private CommonPopupWindow popupWindowLeft;
        private CommonPopupWindow popupWindowRight;
        private CommonPopupWindow popupWindowBottom;
        private CommonPopupWindow popupWindowTop;
        private CommonPopupWindow popupWindow;
    
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
        }
    
    
        private void initView() {
            btn1 = findViewById(R.id.btn1);
            btn2 = findViewById(R.id.btn2);
            btn3 = findViewById(R.id.btn3);
            btn4 = findViewById(R.id.btn4);
            btn5 = findViewById(R.id.btn5);
            content = findViewById(R.id.content);
    
            /**
             * 左侧
             */
            btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showAllLeft(btn1);
                }
            });
    
            /**
             * 右边边弹出框
             */
            btn2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showAllRight(btn2);
                }
            });
    
            /**
             * 中间弹出框
             */
            btn3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showAllBottom(btn3);
                }
            });
    
            /**
             * 全屏弹出
             */
            btn4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showAllWindow();
                }
            });
    
            btn5.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showAllWindowTop(btn5);
                }
            });
        }
    
    
        /**
         * 左侧弹出
         *
         * @param view
         */
        public void showAllLeft(View view) {
    
            try {
                if (popupWindowLeft != null && popupWindowLeft.isShowing()) {
                    return;
                }
                popupWindowLeft = new CommonPopupWindow.Builder(MainActivity.this)
                        .setView(R.layout.pop_left)
                        //定宽
                        .setWidthAndHeight(view.getMeasuredWidth(), ViewGroup.LayoutParams.WRAP_CONTENT)
                        //取值范围0.0f-1.0f 值越小越暗
                        .setBackGroundLevel(0.5f)
                        .setAnimationStyle(R.style.AnimLeft)
                        //true 点击外侧消失,false 点击外侧不消失
                        .setOutsideTouchable(true)
                        .setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                            @Override
                            public void getChildView(View view, int layoutResId) {
    
                                //1
                                view.findViewById(R.id.tv1).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Toast.makeText(MainActivity.this, "Android", Toast.LENGTH_SHORT).show();
                                    }
                                });
                                //2
                                view.findViewById(R.id.tv2).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Toast.makeText(MainActivity.this, "JAVA", Toast.LENGTH_SHORT).show();
                                    }
                                });
                                //取消
                                view.findViewById(R.id.pop_cancle).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
                                        popupWindowLeft.dismiss();
                                    }
                                });
                            }
                        })
                        .create();
    
                popupWindowLeft.showAsDropDown(view);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    
        /**
         * 右侧弹出
         *
         * @param view
         */
        public void showAllRight(View view) {
    
            try {
                if (popupWindowRight != null && popupWindowRight.isShowing()) {
                    return;
                }
                popupWindowRight = new CommonPopupWindow.Builder(MainActivity.this)
                        .setView(R.layout.pop_left)
                        //定宽
                        .setWidthAndHeight(view.getMeasuredWidth(), ViewGroup.LayoutParams.WRAP_CONTENT)
                        .setBackGroundLevel(0.5f)//取值范围0.0f-1.0f 值越小越暗
                        .setAnimationStyle(R.style.AnimRight)
                        //true 点击外侧消失,false 点击外侧不消失
                        .setOutsideTouchable(true)
                        .setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                            @Override
                            public void getChildView(View view, int layoutResId) {
    
                                //1
                                view.findViewById(R.id.tv1).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Toast.makeText(MainActivity.this, "Android", Toast.LENGTH_SHORT).show();
                                    }
                                });
                                //2
                                view.findViewById(R.id.tv2).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Toast.makeText(MainActivity.this, "JAVA", Toast.LENGTH_SHORT).show();
                                    }
                                });
                                //取消
                                view.findViewById(R.id.pop_cancle).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
                                        popupWindowRight.dismiss();
                                    }
                                });
                            }
                        })
                        .create();
                //显示
                popupWindowRight.showAsDropDown(view);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    
        /**
         * 中间弹出框
         *
         * @param view
         */
        public void showAllBottom(View view) {
    
            try {
                if (popupWindowBottom != null && popupWindowBottom.isShowing()) {
                    return;
                }
                popupWindowBottom = new CommonPopupWindow.Builder(MainActivity.this)
                        .setView(R.layout.pop_left)
                        //定宽
                        .setWidthAndHeight(view.getMeasuredWidth(), ViewGroup.LayoutParams.WRAP_CONTENT)
                        .setBackGroundLevel(0.5f)//取值范围0.0f-1.0f 值越小越暗
                        .setAnimationStyle(R.style.AnimMiddle)
                        //true 点击外侧消失,false 点击外侧不消失
                        .setOutsideTouchable(true)
                        .setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                            @Override
                            public void getChildView(View view, int layoutResId) {
    
                                //1
                                view.findViewById(R.id.tv1).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Toast.makeText(MainActivity.this, "Android", Toast.LENGTH_SHORT).show();
                                    }
                                });
                                //2
                                view.findViewById(R.id.tv2).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Toast.makeText(MainActivity.this, "JAVA", Toast.LENGTH_SHORT).show();
                                    }
                                });
                                //取消
                                view.findViewById(R.id.pop_cancle).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
                                        popupWindowBottom.dismiss();
                                    }
                                });
                            }
                        })
                        .create();
                //显示
                popupWindowBottom.showAsDropDown(view);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    
        /**
         * 全屏弹出
         */
        public void showAllWindow() {
    
            try {
                if (popupWindow != null && popupWindow.isShowing()) {
                    return;
                }
    
                popupWindow = new CommonPopupWindow.Builder(MainActivity.this)
                        .setView(R.layout.pop_select_model_icon)
                        //全屏
                        .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                        .setBackGroundLevel(0.5f)//取值范围0.0f-1.0f 值越小越暗
                        .setAnimationStyle(R.style.AnimDown)
                        //true 点击外侧消失,false 点击外侧不消失
                        .setOutsideTouchable(true)
                        .setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                            @Override
                            public void getChildView(View view, int layoutResId) {
    
                                //1
                                view.findViewById(R.id.tv1).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Toast.makeText(MainActivity.this, "超清", Toast.LENGTH_SHORT).show();
                                    }
                                });
                                //2
                                view.findViewById(R.id.tv2).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Toast.makeText(MainActivity.this, "高清", Toast.LENGTH_SHORT).show();
                                    }
                                });
                                //取消
                                view.findViewById(R.id.pop_cancle).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
                                        popupWindow.dismiss();
                                    }
                                });
                            }
                        })
                        .create();
    
                //view 随便一个父类窗体
                popupWindow.showAtLocation(content, Gravity.BOTTOM, 0, 0);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    
        /**
         * 上方弹出框
         *
         * @param view
         */
        public void showAllWindowTop(View view) {
    
            try {
                if (popupWindowTop != null && popupWindowTop.isShowing()) {
                    return;
                }
                popupWindowTop = new CommonPopupWindow.Builder(MainActivity.this)
                        .setView(R.layout.pop_left)
                        //定宽
                        .setWidthAndHeight(view.getMeasuredWidth(), ViewGroup.LayoutParams.WRAP_CONTENT)
                        .setBackGroundLevel(0.5f)//取值范围0.0f-1.0f 值越小越暗
                        .setAnimationStyle(R.style.AnimTop)
                        //true 点击外侧消失,false 点击外侧不消失
                        .setOutsideTouchable(true)
                        .setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                            @Override
                            public void getChildView(View view, int layoutResId) {
    
                                //1
                                view.findViewById(R.id.tv1).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Toast.makeText(MainActivity.this, "Android", Toast.LENGTH_SHORT).show();
                                    }
                                });
                                //2
                                view.findViewById(R.id.tv2).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Toast.makeText(MainActivity.this, "JAVA", Toast.LENGTH_SHORT).show();
                                    }
                                });
                                //取消
                                view.findViewById(R.id.pop_cancle).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
                                        popupWindowTop.dismiss();
                                    }
                                });
                            }
                        })
                        .create();
                //弹窗高度和view本身高度
                int allHeight = popupWindowTop.getHeight() + view.getMeasuredHeight();
                Log.d("LUO", "高度====" + allHeight);
                //显示
                popupWindowTop.showAsDropDown(view, 0, -allHeight);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    

    2. activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <Button
            android:id="@+id/btn1"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_50"
            android:layout_marginLeft="@dimen/dp_15"
            android:layout_marginRight="@dimen/dp_200"
            android:text="左边弹出框" />
        <Button
            android:id="@+id/btn2"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_50"
            android:layout_marginLeft="@dimen/dp_280"
            android:layout_marginRight="@dimen/dp_15"
            android:text="右边边弹出框" />
        <Button
            android:id="@+id/btn3"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_50"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="@dimen/dp_50"
            android:layout_marginRight="@dimen/dp_50"
            android:text="中间弹出框" />
        <Button
            android:id="@+id/btn4"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_50"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="@dimen/dp_15"
            android:layout_marginRight="@dimen/dp_15"
            android:text="全屏弹出框" />
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <Button
                android:id="@+id/btn5"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_50"
                android:layout_alignParentBottom="true"
                android:layout_gravity="center_horizontal"
                android:layout_marginLeft="@dimen/dp_80"
                android:layout_marginRight="@dimen/dp_80"
                android:layout_marginBottom="@dimen/dp_15"
                android:text="上方弹出框" />
        </RelativeLayout>
    
    </LinearLayout>
    

    3. CommonPopupWindow .java

    package com.sumansoul.popupwindowdemo.popwindow;
    
    import android.content.Context;
    import android.view.View;
    import android.widget.PopupWindow;
    
    /**
     * Created by MQ on 2017/5/2.
     */
    public class CommonPopupWindow extends PopupWindow {
        final PopupController controller;
    
        @Override
        public int getWidth() {
            return controller.mPopupView.getMeasuredWidth();
        }
    
        @Override
        public int getHeight() {
            return controller.mPopupView.getMeasuredHeight();
        }
    
        public interface ViewInterface {
            void getChildView(View view, int layoutResId);
        }
    
        private CommonPopupWindow(Context context) {
            controller = new PopupController(context, this);
        }
    
        @Override
        public void dismiss() {
            super.dismiss();
            controller.setBackGroundLevel(1.0f);
        }
    
        public static class Builder {
            private final PopupController.PopupParams params;
            private ViewInterface listener;
    
            public Builder(Context context) {
                params = new PopupController.PopupParams(context);
            }
    
            /**
             * @param layoutResId 设置PopupWindow 布局ID
             * @return Builder
             */
            public Builder setView(int layoutResId) {
                params.mView = null;
                params.layoutResId = layoutResId;
                return this;
            }
    
            /**
             * @param view 设置PopupWindow布局
             * @return Builder
             */
            public Builder setView(View view) {
                params.mView = view;
                params.layoutResId = 0;
                return this;
            }
    
            /**
             * 设置子View
             *
             * @param listener ViewInterface
             * @return Builder
             */
            public Builder setViewOnclickListener(ViewInterface listener) {
                this.listener = listener;
                return this;
            }
    
            /**
             * 设置宽度和高度 如果不设置 默认是wrap_content
             *
             * @param width 宽
             * @return Builder
             */
            public Builder setWidthAndHeight(int width, int height) {
                params.mWidth = width;
                params.mHeight = height;
                return this;
            }
    
            /**
             * 设置背景灰色程度
             *
             * @param level 0.0f-1.0f
             * @return Builder
             */
            public Builder setBackGroundLevel(float level) {
                params.isShowBg = true;
                params.bg_level = level;
                return this;
            }
    
            /**
             * 是否可点击Outside消失
             *
             * @param touchable 是否可点击
             * @return Builder
             */
            public Builder setOutsideTouchable(boolean touchable) {
                params.isTouchable = touchable;
                return this;
            }
    
            /**
             * 设置动画
             *
             * @return Builder
             */
            public Builder setAnimationStyle(int animationStyle) {
                params.isShowAnim = true;
                params.animationStyle = animationStyle;
                return this;
            }
    
            public CommonPopupWindow create() {
                final CommonPopupWindow popupWindow = new CommonPopupWindow(params.mContext);
                params.apply(popupWindow.controller);
                if (listener != null && params.layoutResId != 0) {
                    listener.getChildView(popupWindow.controller.mPopupView, params.layoutResId);
                }
                measureWidthAndHeight(popupWindow.controller.mPopupView);
                return popupWindow;
            }
        }
    
        public static  void measureWidthAndHeight(View view) {
            int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            view.measure(widthMeasureSpec, heightMeasureSpec);
        }
    }
    

    4. PopupController .java

    package com.sumansoul.popupwindowdemo.popwindow;
    
    import android.app.Activity;
    import android.content.Context;
    import android.graphics.drawable.ColorDrawable;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.PopupWindow;
    
    /**
     * Created by MQ on 2017/5/2.
     */
    
    class PopupController {
        private int layoutResId;//布局id
        private Context context;
        private PopupWindow popupWindow;
        View mPopupView;//弹窗布局View
        private View mView;
        private Window mWindow;
    
        PopupController(Context context, PopupWindow popupWindow) {
            this.context = context;
            this.popupWindow = popupWindow;
        }
    
        public void setView(int layoutResId) {
            mView = null;
            this.layoutResId = layoutResId;
            installContent();
        }
    
        public void setView(View view) {
            mView = view;
            this.layoutResId = 0;
            installContent();
        }
    
        private void installContent() {
            if (layoutResId != 0) {
                mPopupView = LayoutInflater.from(context).inflate(layoutResId, null);
            } else if (mView != null) {
                mPopupView = mView;
            }
            popupWindow.setContentView(mPopupView);
        }
    
        /**
         * 设置宽度
         *
         * @param width  宽
         * @param height 高
         */
        private void setWidthAndHeight(int width, int height) {
            if (width == 0 || height == 0) {
                //如果没设置宽高,默认是WRAP_CONTENT
                popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
                popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            } else {
                popupWindow.setWidth(width);
                popupWindow.setHeight(height);
            }
        }
    
    
        /**
         * 设置背景灰色程度
         *
         * @param level 0.0f-1.0f
         */
        void setBackGroundLevel(float level) {
            mWindow = ((Activity) context).getWindow();
            WindowManager.LayoutParams params = mWindow.getAttributes();
            params.alpha = level;
            mWindow.setAttributes(params);
        }
    
    
        /**
         * 设置动画
         */
        private void setAnimationStyle(int animationStyle) {
            popupWindow.setAnimationStyle(animationStyle);
        }
    
        /**
         * 设置Outside是否可点击
         *
         * @param touchable 是否可点击
         */
        private void setOutsideTouchable(boolean touchable) {
            popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));//设置透明背景
            popupWindow.setOutsideTouchable(touchable);//设置outside可点击
            popupWindow.setFocusable(touchable);
        }
    
    
        static class PopupParams {
            public int layoutResId;//布局id
            public Context mContext;
            public int mWidth, mHeight;//弹窗的宽和高
            public boolean isShowBg, isShowAnim;
            public float bg_level;//屏幕背景灰色程度
            public int animationStyle;//动画Id
            public View mView;
            public boolean isTouchable = true;
    
            public PopupParams(Context mContext) {
                this.mContext = mContext;
            }
    
            public void apply(PopupController controller) {
                if (mView != null) {
                    controller.setView(mView);
                } else if (layoutResId != 0) {
                    controller.setView(layoutResId);
                } else {
                    throw new IllegalArgumentException("PopupView's contentView is null");
                }
                controller.setWidthAndHeight(mWidth, mHeight);
                controller.setOutsideTouchable(isTouchable);//设置outside可点击
                if (isShowBg) {
                    //设置背景
                    controller.setBackGroundLevel(bg_level);
                }
                if (isShowAnim) {
                    controller.setAnimationStyle(animationStyle);
                }
            }
        }
    }
    

    5. pop_left.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
    
        <LinearLayout
            android:orientation="vertical"
            android:background="@drawable/round_12"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
    
    
            <TextView
                android:id="@+id/tv1"
                android:gravity="center"
                android:text="Android"
                android:textSize="@dimen/dp_16"
                android:textColor="@color/color_FFFA6F5B"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_50" />
    
            <View
                android:background="@color/color_FFEEEEEE"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_1"/>
            <TextView
                android:gravity="center"
                android:text="JAVA"
                android:textColor="@color/color_FFFA6F5B"
                android:textSize="@dimen/dp_16"
                android:id="@+id/tv2"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_50" />
    
            <View
                android:background="@color/color_FFEEEEEE"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_1"/>
            <TextView
                android:gravity="center"
                android:text="IOS"
                android:textColor="@color/color_FFFA6F5B"
                android:textSize="@dimen/dp_16"
                android:id="@+id/tv3"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_50" />
    
            <View
                android:background="@color/color_FFEEEEEE"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_1"/>
            <TextView
                android:gravity="center"
                android:text="C#"
                android:textColor="@color/color_FFFA6F5B"
                android:textSize="@dimen/dp_16"
                android:id="@+id/tv4"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_50" />
    
        </LinearLayout>
       
        <TextView
            android:id="@+id/pop_cancle"
            android:background="@drawable/round_12"
            android:layout_marginTop="@dimen/dp_10"
            android:gravity="center"
            android:textSize="@dimen/dp_16"
            android:textColor="@color/color_ffcccccc"
            android:text="取消"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_50" />
    </LinearLayout>
    

    6. pop_select_model_icon.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
    
        <LinearLayout
            android:layout_marginLeft="@dimen/dp_10"
            android:layout_marginRight="@dimen/dp_10"
            android:orientation="vertical"
            android:background="@drawable/round_12"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
    
            <TextView
                android:id="@+id/tv1"
                android:gravity="center"
                android:text="超清"
                android:textSize="@dimen/dp_16"
                android:textColor="@color/color_FFFA6F5B"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_50" />
    
            <View
                android:background="@color/color_FFEEEEEE"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_1"/>
            <TextView
                android:gravity="center"
                android:text="高清"
                android:textColor="@color/color_FFFA6F5B"
                android:textSize="@dimen/dp_16"
                android:id="@+id/tv2"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_50" />
    
            <View
                android:background="@color/color_FFEEEEEE"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_1"/>
            <TextView
                android:gravity="center"
                android:text="标清"
                android:textColor="@color/color_FFFA6F5B"
                android:textSize="@dimen/dp_16"
                android:id="@+id/tv3"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_50" />
    
            <View
                android:background="@color/color_FFEEEEEE"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_1"/>
            <TextView
                android:gravity="center"
                android:text="流畅"
                android:textColor="@color/color_FFFA6F5B"
                android:textSize="@dimen/dp_16"
                android:id="@+id/tv4"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_50" />
    
        </LinearLayout>
        
        <TextView
            android:layout_marginLeft="@dimen/dp_10"
            android:layout_marginRight="@dimen/dp_10"
            android:id="@+id/pop_cancle"
            android:layout_marginBottom="@dimen/dp_10"
            android:background="@drawable/round_12"
            android:layout_marginTop="@dimen/dp_10"
            android:gravity="center"
            android:textSize="@dimen/dp_16"
            android:textColor="@color/color_ffcccccc"
            android:text="取消"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_50" />
    
    </LinearLayout>
    

    7. styles.xml

     <style name="AnimTop" parent="@android:style/Animation">
            <item name="android:windowEnterAnimation">@anim/top_in</item>
            <item name="android:windowExitAnimation">@anim/top_out</item>
        </style>
    
        <style name="AnimRight" parent="@android:style/Animation">
            <item name="android:windowEnterAnimation">@anim/right_in</item>
            <item name="android:windowExitAnimation">@anim/right_out</item>
        </style>
    
        <style name="AnimLeft" parent="@android:style/Animation">
            <item name="android:windowEnterAnimation">@anim/left_in</item>
            <item name="android:windowExitAnimation">@anim/left_out</item>
        </style>
    
        <style name="AnimMiddle" parent="@android:style/Animation">
            <item name="android:windowEnterAnimation">@anim/middle_in</item>
            <item name="android:windowExitAnimation">@anim/middle_out</item>
        </style>
    
        <style name="AnimDown" parent="@android:style/Animation">
            <item name="android:windowEnterAnimation">@anim/bottom_in</item>
            <item name="android:windowExitAnimation">@anim/bottom_out</item>
        </style>
    

    8. left_in.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
    
        <scale
            android:duration="500"
            android:fillAfter="false"
            android:fromXScale="0.0"
            android:fromYScale="0.0"
            android:interpolator="@android:anim/accelerate_decelerate_interpolator"
            android:pivotX="0%"
            android:pivotY="0%"
            android:toXScale="1.0"
            android:toYScale="1.0" />
    
    </set>
    

    9. left_out.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
    
        <scale
            android:duration="500"
            android:fillAfter="false"
            android:fromXScale="1.0"
            android:fromYScale="1.0"
            android:interpolator="@android:anim/accelerate_decelerate_interpolator"
            android:pivotX="0%"
            android:pivotY="0%"
            android:toXScale="0.0"
            android:toYScale="0.0" />
    
    </set>
    

    10. right_in.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
    
        <scale
            android:duration="500"
            android:fillAfter="false"
            android:fromXScale="0.0"
            android:fromYScale="0.0"
            android:interpolator="@android:anim/accelerate_decelerate_interpolator"
            android:pivotX="100%"
            android:pivotY="0%"
            android:toXScale="1.0"
            android:toYScale="1.0" />
    
    </set>
    

    11. right_out.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <scale
            android:duration="500"
            android:fillAfter="false"
            android:fromXScale="1.0"
            android:fromYScale="1.0"
            android:interpolator="@android:anim/accelerate_decelerate_interpolator"
            android:pivotX="100%"
            android:pivotY="0%"
            android:toXScale="0.0"
            android:toYScale="0.0" />
    
    </set>
    

    12. middle_in.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
    
        <scale
            android:duration="500"
            android:fillAfter="false"
            android:fromXScale="0.0"
            android:fromYScale="0.0"
            android:interpolator="@android:anim/accelerate_decelerate_interpolator"
            android:pivotX="50%"
            android:pivotY="0%"
            android:toXScale="1.0"
            android:toYScale="1.0" />
    
    </set>
    

    13. middle_out.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <scale
            android:duration="500"
            android:fillAfter="false"
            android:fromXScale="1.0"
            android:fromYScale="1.0"
            android:interpolator="@android:anim/accelerate_decelerate_interpolator"
            android:pivotX="50%"
            android:pivotY="0%"
            android:toXScale="0.0"
            android:toYScale="0.0" />
    
    </set>
    

    14. top_in.xml

    <?xml version="1.0" encoding="utf-8"?>
    <!-- 从屏幕上面进入 -->
    <set xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <translate
            android:duration="500"
            android:fromYDelta="-100%p"
            android:toYDelta="0" />
    
    </set>
    

    14. top_out.xml

    <?xml version="1.0" encoding="utf-8"?>
    <!-- 从屏幕上面退出 -->
    <set xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <translate
            android:duration="500"
            android:fromYDelta="0"
            android:toYDelta="-100%p" />
    </set>
    

    15. bottom_in.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromYDelta="50%p"
        android:toYDelta="0" />
    </set>
    

    15. bottom_out.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
    
        <translate
            android:duration="500"
            android:fromYDelta="0"
            android:toYDelta="50%p" />
    </set>
    

    參考链接:https://blog.csdn.net/kai_zone/article/details/80198447

    相关文章

      网友评论

          本文标题:Android popupWindow的使用

          本文链接:https://www.haomeiwen.com/subject/sfdnohtx.html