Android开发使用PopupWindow在指定View上/下/左/右显示(菜单在指定位置显示)

PupopWindow动态获取显示的位置,并添加指示箭头 
效果图

项目地址:(欢迎star)

使用方法

     mLucklyPopopWindow = new LucklyPopopWindow(this);
        //给popupWindow添加数据
        mLucklyPopopWindow.setData(getResources().getStringArray(R.array.popupArray), new int[]{R.mipmap.add, R.mipmap.delete, R.mipmap.modify, R.mipmap.update});

        mAdapter.setOnItemClickListener(new RecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //必须设置宽度
                mLucklyPopopWindow.setWidth(150);
                //监听事件
                mLucklyPopopWindow.setOnItemClickListener(new LucklyPopopWindow.OnItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        Toast.makeText(MainActivity.this, "点击的位置" + position, Toast.LENGTH_SHORT).show();
                        mLucklyPopopWindow.dismiss();
                    }
                });

                //添加分割线(可选)
                mLucklyPopopWindow.addItemDecoration(LucklyPopopWindow.VERTICAL,Color.GRAY,1);
                //设置image不显示(可选)
               // mLucklyPopopWindow.setImageDisable(true);
                //设置image的大小(可选)
                mLucklyPopopWindow.setImageSize(20,20);
                //显示popopWindow
                mLucklyPopopWindow.show(getWindow().getDecorView(), view);

            }
        });


常用的方法

1、添加数据

   添加数据的时候,内容和图片的个数应该相同;如果不需要添加图片的话,那么使用第一个和第四个方法,传递的Bitmap=null即可。

     void setData(DataBeans[] strings);
     void setData(String[] data, int[] images);
     void setData(String[] data, Bitmap[] images);
     void setData(List<DataBeans> list);

2、设置LucklyPopupWindow的宽度(必须设置)

   LucklyPopupWindow的宽度(必须设置);设置的单位是dp。

     void setWidth(int widthDp);

3、给每一个Item添加分割线

   默认的情况是没有分割线的。需要调用以下方法。

     //可以自己添加RecyclerView的分割线
     addItemDecoration(RecyclerView.ItemDecoration itemDecoration);
     //使用内部封装好了的分割线,传入的参数分别是:方向,颜色,分割线的宽
     addItemDecoration(int oritation, int color, int lineHeight);

4、设置背景颜色

   也就是设置三角形和矩形框的背景颜色

     setBackgroundColor(int backgroundColor);

5、设置PopupWindow显示时Activity其余部分显示灰色程度

   取值范围0.0<=darkBackgroundDegree<=1.0f

     setDarkBackgroundDegree(float darkBackgroundDegree);

6、设置字体的颜色和大小

     setTextColor(int textColor);
     setTextSize(int textSize);

7、设置图片不显示以及设置图片大小

     setImageDisable(boolean imageDisable);
     setImageSize(int widthDp,int heightDp);

8、添加监听事件

     void setOnItemClickListener(LucklyPopopWindow.OnItemClickListener onItemClickListener);

9、设置箭头的宽,高,圆角矩形的半径

     void setTriangleWidth(int triangleWidth);
     void setTrianleHeight(int trianleHeight);
     void setRadius(int radius);

10、在某个View下/上显示(自动判断上下)

   注意:这个方法必须最后调用。

    void show(View parentView, View positionView);

LucklyPopouWindow的使用方法。

  请见: https://github.com/MrGaoGang/luckly_popup_window   欢迎Star

猜你喜欢

转载自blog.csdn.net/qq_32400821/article/details/79161596
今日推荐