Android使用PopUpWindow

PopUpWindow的使用

据说PopUpwindow可以在任意Activity的上方实现。

功能设想:

1、在Fragment上显示一个PopUpwindow作为进程指示
2、点击ListView中项目,在PopUpwindow中加载项目对应的文件 3、在PopUpwindow中显示操作提示

参考:

PopUpwindow简单显示实现

核心代码:

private void showPopUpWindow(){
        //代码实现
	mPopupView = getLayoutInflater().inflate(R.layout.popupwindow_testing,null);
    mPopupWindow = new PopupWindow(mPopupView,ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
    mPopupWindow.setOutsideTouchable(true);
    mPopupWindow.showAtLocation(mPopupView,
                Gravity.CENTER|Gravity.CENTER_HORIZONTAL,0,0);
    }

说明:先将布局文件转换为View对象,再指定PopUpwindow相关属性(位置、点击等)

一些升级

屏幕背景变半透明

想要除PopUpwindow外的窗口变成半透明,只需要获取窗口,然后设置透明属性就行。

private void addBackground(){
        layoutParams = ((Activity)context).getWindow().getAttributes();
        layoutParams.alpha = 0.4f;
        ((Activity)context).getWindow().setAttributes(layoutParams);
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams layoutParams = ((Activity)context).getWindow().getAttributes();
                layoutParams.alpha = 1f;
                ((Activity)context).getWindow().setAttributes(layoutParams);
            }
        });
    }

遇到的一些坑:

1、在Fragment中不能直接使用getWindow()获取窗口

由于Fragment寄生于Activity,想要使用getWindow()就要先getActivity() 【Android Fragment and getWindow()】

WindowManager.LayoutParams layoutParams = getActivity().getWindow().getAttributes();
2、在设置半透明效果的时候getWindow()问题

直接使用getActivity().getWindow()无法获取背景Fragment使其变暗。
由于我这里使用的活动是Fragment,在获取窗口就出了些问题,要用传入的context强制转换为Activity后再getWindow()才可以【我的背景为什么不变暗?】

layoutParams = ((Activity)context).getWindow().getAttributes();
layoutParams.alpha = 0.4f;
((Activity)context).getWindow().setAttributes(layoutParams);
3、华为手机无法显示背景变暗效果????

我用华为荣耀7作为测试手机,无论怎么调,就是无法显示变暗效果,在魅蓝note2上测试就没有问题,可以正常显示变暗效果。

最后,给PopupWindow封装了一个类

可以直接复制到工程中调用,按照注释传入上下文即可
效果图
在这里插入图片描述

注意由于我这里是在Fragment中调用,因此传入的context经过了强制转换为Activity,不知道在Activity中调用会不会有什么问题,大家可以试一下
另外我这里只实现了简单的加载显示,需要更多的效果的,可以直接在这个类中修改。

package aaa.bb.cc.dd.uiUtil;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

/**
 * 封装了PopupWindow的一个实体类
 * Created by ancheng on 2019/11/27.
 */

public class PopupWindowUtil {
    private PopupWindow mPopupWindow;
    private View mPopupView;
    private WindowManager.LayoutParams mLayoutParams;
    private Context mContext;

    /**
     * 构造函数
     * @param mContext 界面上下文
     */
    public  PopupWindowUtil(Context mContext){
        this.mContext = mContext;
    }

    /**
     * 检测开始时加载一个窗口告诉用户正在检测
     * @param mPopupWindowLayoutID 布局文件的ID
     */
    public void showPopUpWindow(int mPopupWindowLayoutID){
        //代码实现
        mPopupView = ((Activity)mContext).getLayoutInflater().inflate(mPopupWindowLayoutID,null);
        mPopupWindow = new PopupWindow(mPopupView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        addBackground();
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.showAtLocation(mPopupView,
                Gravity.CENTER|Gravity.CENTER_HORIZONTAL,0,0);
    }

    public void dismissPopupWindow(){
        mPopupWindow.dismiss();
    }
    /**
     * 检测开始时加载一个窗口告诉用户正在检测
     * @param mPopupWindowLayoutID 布局文件的ID
     * @param isOutsideTouchable 外部是否可以点击
     */
    public void showPopUpWindow(int mPopupWindowLayoutID,boolean isOutsideTouchable){
        //代码实现
        mPopupView = ((Activity)mContext).getLayoutInflater().inflate(mPopupWindowLayoutID,null);
        mPopupWindow = new PopupWindow(mPopupView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        addBackground();
        mPopupWindow.setOutsideTouchable(isOutsideTouchable);
        mPopupWindow.showAtLocation(mPopupView,
                Gravity.CENTER|Gravity.CENTER_HORIZONTAL,0,0);
    }

    /**
     * 添加一个背景变暗效果
     */
    private void addBackground(){
        //这里要使用context强制转换到Activity
        mLayoutParams = ((Activity)mContext).getWindow().getAttributes();
        mLayoutParams.alpha = 0.4f;
        ((Activity)mContext).getWindow().setAttributes(mLayoutParams);
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams layoutParams = ((Activity)mContext).getWindow().getAttributes();
                layoutParams.alpha = 1f;
                ((Activity)mContext).getWindow().setAttributes(layoutParams);
            }
        });
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值