Android-PopupWindow自定义的弹出框

PopupWindow实战
本文详细介绍了如何使用PopupWindow自定义弹出框,包括布局文件的设计、响应事件的处理及动画效果的实现等关键技术点。

PopupWindow自定义的弹出框的使用记录

使用介绍

  • 可实现弹出框的效果
  • 这里是将PopupWindow添加到ActionBar下,通过点击标题上的图标显示

使用步骤

  1. 新建一个PopupWindow的xml布局(popup_window.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="wrap_content"
    android:orientation="vertical">
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="PopupWindow\n出来了\n出来了\n出来了" />
    
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="点击了Popup" />
    </LinearLayout>
  2. 在Activity代码中

    • 响应actionbar上子控件的点击事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    
        switch (item.getItemId()) {
            case R.id.city_choose://当点击这个item时显示PopupWindow
                //处理显示PopupWindow
                showPopupWindow();
                return true;
            case R.id.get_location:
                getpostion();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
    • showPopupWindow()设置显示PopupWindow的方法
    private void showPopupWindow() {
        // 获取状态栏高度
        Rect frame = new Rect();
        getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        //状态栏高度:frame.top; toolbar是我自定义的标题栏,如果是默认的可以直接用getActionBar.getHeight()
        int xOffset = frame.top + toolbar.getHeight() - 25;//减去阴影宽度,适配UI.
        int yOffset = Dp2Px(this, 5f); //设置x方向offset为5dp
        //加载的Activity的布局R.layout.activity_weather
        View parentView = getLayoutInflater().inflate(R.layout.activity_weather, null);
        //获取PopupWindow的布局
        View contentView = LayoutInflater.from(this).inflate(R.layout.popup_window, null);
        //获取和设置PopupWindow布局里button的监听
        Button button = (Button) contentView.findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(WeatherActivity.this, "button is pressed",
                        Toast.LENGTH_SHORT).show();
            }
        });
        //创建一个PopupWindow对象
        //参数一:PopupWindow的布局;
        //参数二:宽度(注意导入的包是:android.view.ViewGroup.LayoutParams;)
        //参数三:高度
        //参数四:设置focusable
        PopupWindow popupWindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
//        popupWindow.setTouchable(true);
//        popupWindow.setFocusable(true);
//        popupWindow.setOutsideTouchable(true);
//能监听到PopupWindow被点击
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.i("myout", "ontouch");
                return false;
            }
        });

//必须设置BackgroundDrawable 后 ,点击外部可以消失的方法setOutsideTouchable(true)才会有效。这里在XML中定义背景,所以这里设置为null;
//        popupWindow.showAsDropDown(view);显示在哪个布局的下方(一般当不在标题栏的时候 使用)
        popupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(),
(Bitmap) null));
        popupWindow.setOutsideTouchable(true); //点击外部关闭。
        popupWindow.setAnimationStyle(android.R.style.Animation_Dialog);    //设置一个动画。
        //设置Gravity,让它显示在右上角。
        popupWindow.showAtLocation(parentView, Gravity.RIGHT | Gravity.TOP,
                yOffset, xOffset);
    }
        /**
        * 处理Java代码中多是接受px单位的尺寸,Android用了dp单位
        * Dp转换Px的方法
        */
    public int Dp2Px(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

学习的地址:

我的运行效果图

这里写图片描述

我的感受

  • 使用PopupWindow是因为不想跳到第二个页面搜索城市获取天气(@ ̄ー ̄@)
  • 这个弹出的效果挺好的,当PopupWindow弹出时,点击屏幕任何部位都会调用PopupWindow的setTouchInterceptor()方法

想说的话

  • 博客坚持写,今后学习了新的东西就在这里记录一下,以便今后回顾,也希望小小笔记能帮助你们
  • 若内容有什么地方不对、不清楚,还望吐槽,希望大家能一起成长
  • 来一句:有意识的改变自己的不良的生活方式
  • 再来一句:对于程序员来说,一千多万不在代码里,不在技术里,而在生活中,在眼光里,在勇气里。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值