自定义PopupWindow的使用

自定义的弹框一般用在展示信息部分的每一个item的更多选项,点击“更多”从底部弹出,方便用户进行交互!
1.下面是用到的布局文件layout_pop.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/shape_common"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_common"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_follow"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:gravity="center"
            android:text="关注"
            android:textColor="@color/black_33"
            android:textSize="16sp" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="#e1e1e1"></View>

        <TextView
            android:id="@+id/tv_share"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:gravity="center"
            android:text="分享"
            android:textColor="@color/black_33"
            android:background="@drawable/shape_common"
            android:textSize="16sp" />
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="#e1e1e1"></View>

        <TextView
            android:id="@+id/tv_report"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:gravity="center"
            android:text="举报"
            android:textColor="@color/black_33"
            android:background="@drawable/shape_common"
            android:textSize="16sp" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="@color/gray_dc"
        />

    <TextView
        android:id="@+id/tv_cancel"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:gravity="center"
        android:background="@drawable/shape_common"
        android:text="取消"
        android:textColor="@color/black_33"
        android:textSize="16sp" />
</LinearLayout>
2.下面是popupWindow创建过程,以及主要部分代码;
    /**
     * 弹出popupwindow
     */
    private void showPopupWindow(View v) {
        if (popupWindow == null) {
            // 将自己定义的布局文件泵出来
            final View popupWindow_view = LayoutInflater.from(mContext).inflate(
                    R.layout.layout_pop, null, false);
            // 创建PopupWindow实例,LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT分别是宽度和高度
            popupWindow = new PopupWindow(popupWindow_view, ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT, true);
            //弹出的时候设置
            setWindowBackgroundAlpha(0.7f);
            // 设置动画效果
            popupWindow.setAnimationStyle(R.style.AnimationFade);
            popupWindow.setOutsideTouchable(true);
            popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
            // 点击其他地方消失
            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    //背景恢复正常
                    setWindowBackgroundAlpha(1.0f);
                    popupWindow = null;
                }
            });
        }
        // 这里是位置显示方式,在屏幕的底部,0,0分别表示X,Y的偏移量
        popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
    }

    /**
     * 关闭popupWindow
     */

    private void closePopupWindow() {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
            popupWindow = null;
        }
    }

    /**
     * 设置添加屏幕背景透明度
     *
     * @param bgAlpha
     */
    public void setWindowBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
        lp.alpha = bgAlpha;//0.0-1.0
        //有的手机需要加上下面这句,否则无效
        ((Activity) mContext).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        ((Activity) mContext).getWindow().setAttributes(lp);
    }
       因为我是在Adapter里面实现的,所以使用了((Activity) mContext).getWindow()来获取Window对象,大家如果是在Activity里面直接getWindow()就可以了。这样就实现了上面的自定义布局弹框,弹出的时候添加了一个暗黑的透明度,点击其他区域弹框也能小时消失。是不是实现起来很简单呢。大家也练练手吧!
       如果要在指定位置显示,就要好好看 showAtLocation(View parent , int gravity , int x , int y)这个方法的源码,基于父控件进行一定尺寸的偏移,下面我把此方法的源码解释也贴出来,方便大家阅读。
    /**
     * <p>
     * Display the content view in a popup window at the specified location. If the popup window
     * cannot fit on screen, it will be clipped. See {@link android.view.WindowManager.LayoutParams}
     * for more information on how gravity and the x and y parameters are related. Specifying
     * a gravity of {@link android.view.Gravity#NO_GRAVITY} is similar to specifying
     * <code>Gravity.LEFT | Gravity.TOP</code>.
     * </p>
     *
     * @param parent a parent view to get the {@link android.view.View#getWindowToken()} token from
     * @param gravity the gravity which controls the placement of the popup window
     * @param x the popup's x location offset
     * @param y the popup's y location offset
     */
    public void showAtLocation(View parent, int gravity, int x, int y) {
        showAtLocation(parent.getWindowToken(), gravity, x, y);
    }

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值