popupWindow 使用


1、android 对话框常见的两种:dialog,popupWindow。其中dialog是android提供的,包含了很多,现成的定制,同时,功能也受到了限制。普通窗口建议用dialog。

dialog满足不了的用popupWindow,popupWindow 是很自由的自定义显示界面。官方文档的描述是:A popup window that can be used to display an arbitrary view(任意视图). The popup window is a floating container that appears on top of the current activity(显示在当前activity 前).


2、popupWindow使用 参见官网:http://developer.android.com/reference/android/widget/PopupWindow.html

AlertDialog 使用和普通控件类似只要定义在指定activity 的Context,需要的时候show出来就行了。

popopWindow 使用上有区别:1、定义popupwindow关联自己的视图,2、显示popupWindow 需要一个指定视图做参考坐标

它的构造函数有好多个,我用到的是

public PopupWindow (View contentView, int width, int height, boolean focusable)
contentView the popup's content
width the popup's width
height the popup's height
focusable true if the popup can be focused, false otherwise


显示函数用:其中parent 及是 参考坐标视图
public void showAtLocation (View parent, int gravity, int x, int y)
parent a parent view to get the getWindowToken() token from
gravity the gravity which controls the placement of the popup window
x the popup's x location offset
y the popup's y location offset

gracity 可以选择头部,中部(Gravity.CENTER),底部


3、使用注意
popupWindow 默认是不响应内部的响应事件的,所以用了前面说的构造函数,最后一个参数设置为true
这样子还不够必须,添加以下代码:

        // 这两句加了,才能点击并且响应
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());// 响应返回键必须的语句。
        mPopupWindow.setOutsideTouchable(true);// 点击外部按钮 取消。按返回按键也取消

在popupwindow的xml界面 外部布局里可以添加android:focusableInTouchMode="true"   (不是必须的,如果已经有添加上面的几步的话)

4、如果popupwindow里有edit的话可以添加以下代码,增强用户体验,及点击空白区域,缩下键盘
更好的方式:
 首先让edit失去焦点
让其父类控件获取焦点(就不会一进入就获取键盘)
            android:focusable="true"
            android:focusableInTouchMode="true"

其次重写
@Override
public boolean onTouchEvent(MotionEvent event) {

InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
return super.onTouchEvent(event);

}


普通方式:



popView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                InputMethodManager imm = (InputMethodManager) mContext
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                // login_auto_account.setCursorVisible(false);
                imm.hideSoftInputFromWindow(edit_mony.getWindowToken(), 0);
            }
        });


 


5、精确设置popwindow 大小
/*
     * 带测量popwindow高度的方法
     */
    @Deprecated
    private void hintPopWindow() {


        final View fatherView = getLayoutInflater().inflate(
                R.layout.activity_set, null);
        
        //外圈布局是指定pop大小的时候会指定出这个大小。所以无法代表原来窗口的大小
        final View popView = getLayoutInflater().inflate(
                R.layout.popwindow_set, null);
        
        //内圈布局-真实窗口大小。到时候测出这个大小  回调函数设置具体的大小
        final View subView = popView.findViewById(R.id.layout_set);
 
        //设置回调函数  在新生成的窗口的瞬间回调该函数  重新写窗口高度  宽度
        ViewTreeObserver vto = popView.getViewTreeObserver();
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {


            public boolean onPreDraw() {


                int h = subView.getMeasuredHeight();
                int w = subView.getMeasuredWidth();


                mPopupWindow.dismiss();
                mPopupWindow.setHeight(h);
                mPopupWindow.showAtLocation(fatherView, Gravity.BOTTOM, 0, 0);// 指定视图偏移


                return true;
            }


        });




        int width = (int) (getWindowManager().getDefaultDisplay().getWidth());
        int height = (int) (getWindowManager().getDefaultDisplay().getHeight());


        Log.d("11", height + " -----height");


        // mDatePicker.set
        mPopupWindow = new PopupWindow(popView, width, height, true);// 自己视图


        // 这两句加了,才能点击并且响应
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());// 响应返回键必须的语句。
        mPopupWindow.setOutsideTouchable(true);// 点击外部按钮 取消。按返回按键也取消


        // 显示视图与指定视图之间的偏差
        // mPopupWindow.showAsDropDown(mListView);//指定视图下方
        mPopupWindow.showAtLocation(fatherView, Gravity.BOTTOM, 0, 0);// 指定视图偏移
    }

ex:

private void updatePopwindows(Activity context, View view) {

DisplayMetrics dm = new DisplayMetrics();

 

context.getWindowManager().getDefaultDisplay().getMetrics(dm);

int width = dm.widthPixels;

int height = dm.heightPixels;


View popView = LayoutInflater.from(context).inflate(R.layout.popupwindow_task, null);

TextView logTextView = (TextView) popView.findViewById(R.id.txt_task_info);

 


// mDatePicker.set


final PopupWindow mPopupWindow = new PopupWindow(popView, (int) (width), (int) (height), true);// 自己视图


// 这两句加了,才能点击并且响应

mPopupWindow.setBackgroundDrawable(new BitmapDrawable());// 响应返回键必须的语句。

mPopupWindow.setOutsideTouchable(true);// 点击外部按钮 取消。按返回按键也取消


 

// 显示视图与指定视图之间的偏差

// mPopupWindow.showAsDropDown(mListView);//指定视图下方

mPopupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);// 指定视图偏移

 

popView.findViewById(R.id.imageView_close).setOnClickListener(new OnClickListener() {


@Override

public void onClick(View v) {

// TODO Auto-generated method stub

mPopupWindow.dismiss();

}

});

 

popView.findViewById(R.id.imageView_close).setOnClickListener(new OnClickListener() {

 

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

mPopupWindow.dismiss();

}

});

}



 

转载于:https://www.cnblogs.com/sucerli/p/4358436.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
带模糊特效的选项提示弹出框项目里面的代码有详细注释哦, 如果对你有用请点一下star吧!!!qq原效果加上模糊特效后实现:首先我做出来的自定义view是希望全局只要调一个方法就可以用的, 就像popupwindow那样, 所以我的思路是初始化整个布局, 然后以Toast的方式添加到屏幕最前端.所以第一步: 初始化整个弹窗布局第二步: 这里比较重要了, 当点击button, 弹出选项框的时候, 具体做哪些事情到这里我们所有操作就都完成了 大家是不是感觉很简单, 嘿嘿嘿, 最难的坑其实是模糊图片那里, 因为我们是当用户点击弹出按钮的时候动态模糊的, 所以效率就很重要, 下面是我对activity视图bitmap的处理:当用户点下按钮时,我们需要立刻就将模糊后的图片显示出来, 下面是我的模糊图片代码:android里面的高斯模糊我大概总结了一下 基本有三种, 优缺点都有, 我用的是系统推荐的, 速度比较快,而且也简单, 但只能支持android版本17以上, 但现在手机用android4.2以下的估计也很少了.第二种就是利用glide自定义类继承BitmapTransformation来实现在加载图片时模糊图片,但和第一种差不多,也要android版本17以上才能用第三种就是用java层的代码, 手动算出像素值, 因为图片处理的代码逻辑都是用java实现的, 所以效率极差, 不推荐.最后在说一下那个弹出蠕动的动画, 很简单20行代码就ok了, 我是用属性动画写的, 让弹窗view的宽和高的规模从0到1, 然后在从1到0.95, 这样就造成了一个弹出的动态效果, 很easy吧大功告成出来的效果就是这样的啦
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值