【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>
具体代码
@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()
}
popWindow.isOutsideTouchable = true
popWindow.isFocusable = true
popWindow.setBackgroundDrawable(null)
popWindow.showAtLocation(rl_parent, Gravity.BOTTOM, 0, 0)
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
}
}
|