【Android】【PopupWindow】的使用

【PopupWindow】的使用和【Dialog】差不多,感觉在实际开发中还是使用Dialog多一点。

1.创建布局
view_popupwin.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="178dp"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/ll_return"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="12dp"
            android:layout_height="24dp"
            android:layout_margin="18dp"
            android:background="@drawable/ic_return" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="上传视频"
            android:textColor="@color/cBlackTxt"
            android:textSize="15sp" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/cGrey" />

    <TextView
        android:id="@+id/tv_take_video"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="17dp"
        android:gravity="center"
        android:text="拍视频"
        android:textColor="@color/cBlackTxt"
        android:textSize="18sp"
        android:textStyle="bold" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/cGrey" />

    <TextView
        android:id="@+id/tv_upload_local"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="17dp"
        android:gravity="center"
        android:text="本地上传"
        android:textColor="@color/cBlackTxt"
        android:textSize="18sp"
        android:textStyle="bold" />
</LinearLayout>

具体代码

    /**
     * 打开相机或相册的PopupWindow
     */
    @SuppressLint("ClickableViewAccessibility")
    private fun showLoadView() {
        val popView = LayoutInflater.from(this).inflate(R.layout.view_popupwin, null)
        val popWindow = PopupWindow(this)
        popWindow.contentView = popView
        popWindow.width = ViewGroup.LayoutParams.MATCH_PARENT
        popWindow.height = ViewGroup.LayoutParams.WRAP_CONTENT
        val ll_return = popView.findViewById<LinearLayout>(R.id.ll_return)
        ll_return.setOnClickListener {
            popWindow.dismiss()
            
        }
        val tv_take_video = popView.findViewById<TextView>(R.id.tv_take_video)
        tv_take_video.setOnClickListener {
            popWindow.dismiss()
           
        }
        val tv_upload_local = popView.findViewById<TextView>(R.id.tv_upload_local)
        tv_upload_local.setOnClickListener {
            popWindow.dismiss()
      
        }
        // 设置点击窗口外边窗口消失,必须在ShowAtLocation方法之前调用
        popWindow.isOutsideTouchable = true
        // 设置此参数获得焦点,否则无法点击
        popWindow.isFocusable = true

        popWindow.setBackgroundDrawable(null)//去掉pop四周的黑线
        //popup和dialog的不同就是必须要有依赖的布局控件,设置在这个布局的上下左右等位置
        popWindow.showAtLocation(rl_parent, Gravity.BOTTOM, 0, 0)//设置pop在控件rl_parent底部显示

        //设置popup出现的时候,popup外面区域显示为阴影
        val lp = this.window.attributes
        lp.alpha = 0.7f
        this.window.attributes = lp
        popWindow.setOnDismissListener {
            val lp = this.window.attributes
            lp.alpha = 1f
            this.window.attributes = lp
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值