Android popupWindow和Dialog的使用

本文详细介绍了Android中PopupWindow和Dialog的使用方法,包括布局设置、显示位置的调整,以及两者之间的区别。PopupWindow是非阻塞式,Dialog则是阻塞式的。两者都是附加在Window上的,Dialog的创建过程涉及PhoneWindow、DecorView。文中还提到了通过WindowManager实现可拖动悬浮窗的技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上效果图:分别是弹出dialog和popupwindow
在这里插入图片描述
App开发时,经常会使用到Dialog,Popupwindow做弹窗效果,尽管网上已经有很多封装好的库,但是我们也要知道其基本的使用方法。

一、PopupWindow
package com.example.lytestproject.util;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.lytestproject.R;

public class NotificationPopupWindow extends PopupWindow {

    /**
     * 构造方法
     * @param context 
     * @param listener 
     */
    public NotificationPopupWindow(Context context, View.OnClickListener listener) {
        super(context);

        LayoutInflater inflater = LayoutInflater.from(context);
        final View view = inflater.inflate(R.layout.pop_notification, null);
        setContentView(view);
        /**
         * popupwindow显示之前一定要设置宽高
         * 而dialog没有此限制
         */
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);

        TextView tv_confirm = view.findViewById(R.id.id_confirm);
        tv_confirm.setOnClickListener(listener);
        final CardView cardView = view.findViewById(R.id.id_popview);
        /**
         * 可以给控件加上动画效果
         */
        Animation animation = AnimationUtils.loadAnimation(context,R.anim.translate_anim);
        cardView.startAnimation(animation);
        // 实例化一个ColorDrawable颜色
        ColorDrawable dw = new ColorDrawable(0x10000000);
        // 设置弹出窗体的背景
        this.setBackgroundDrawable(dw);
        /**
         * popupwindow默认不响应back键
         * 除非设置setFocusable(true),点击back键pop会消失
         * Dialog不需要设置,默认响应back键
         */
        setFocusable(true);

        /**
         * 设置点击外部pop消失的范围
         */
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                /**
                 * 获得点击位置的坐标点 x,y
                 * 获得布局文件中cardview控件的getTop,getleft,getright,getbottom
                 * 两者进行比较得到cardView控件之外的范围,也就是点击屏幕pop消失的范围
                 */
                int x = (int) event.getX();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (cardView.getTop() > y || cardView.getBottom() < y || cardView.getLeft() > x || cardView.getRight() < x) {
                        dismiss();
                    }
                }
                return true;
            }
        });
    }
}

布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:id="@+id/id_popview"
        android:layout_width="260dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值