本文主要是介绍Android中实现底部弹窗的的正确姿势,如果你在实现底部弹窗时遇到了一些问题,那么请仔细阅读本文,相信文章会对你有所帮助。
<>强收获早知道强>
阅读完本文后,你可以有以下收获
-
<李>利用PopupWindow实现底部弹窗李>
<李> PopupWindow实现底部弹窗时的缺点李>
<李>解决利用PopupWindow实现底部弹窗,无法覆盖状态栏的问题李>
<李>利用对话框实现底部弹窗李>
<李>利用dialogFragment实现底部弹窗李>
<>强实现底部弹窗的方式强>
由于本人水平有限,只知道一下几种实现底部弹窗的方式
-
<李>利用PopupWindow实现底部弹窗。李>
<李>利用对话框实现底部弹窗。李>
<李>利用DialogFragment实现底部弹窗。李>
下面,就利用以上三种方式分别实现Android中的底部弹窗。
<>强利用PopWindow实现底部弹窗强>
因为本文主要是介绍实现底部弹窗的方式,所以,不会对PopupWindow进行具体的讲解,大家可以到这里了解PopupWindow。
直接进入主题,按照套路,一步步实现利用PopupWindow实现底部弹窗,首先,写一个布局文件作为PopupWindow中的内容,布局文件如下
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt; RelativeLayout xmlns: android=" http://schemas.android.com/apk/res/android " android: layout_width=" match_parent " android:背景=" # 553 b3a3a " android: layout_height=癿atch_parent”比; LinearLayout & lt; android: layout_width=" match_parent " android: layout_alignParentBottom=" true " android:取向=按怪薄? android: id=癅 + id/内容” android:背景=" @android:颜色/白” android: layout_height=皐rap_content”比; & lt; TextView android: layout_width=" match_parent " android:输入textColor=" # 333 " android:文本="相机” android:填充=" 8 dp " android: id=癅 + id/open_from_camera” android:重力="中心" android: textSize=" 15 sp” android: layout_height=?0 dp/比; & lt; TextView android: layout_marginTop=" 1 dp " android: id=癅 + id/open_album” android: layout_width=" match_parent " android:输入textColor=" # 333 " android:文本="打开图库” android:填充=" 8 dp " android:重力="中心" android: textSize=" 15 sp” android: layout_height=?0 dp/比; & lt; TextView android: layout_marginTop=" 1 dp " android: id=癅 + id/取消” android: layout_width=" match_parent " android:输入textColor=" # 333 " android:文本="取消” android:填充=" 8 dp " android:重力="中心" android: textSize=" 15 sp” android: layout_height=?0 dp/比; & lt;/LinearLayout> & lt;/RelativeLayout> >之前注:这里使用的是填充父窗口的方式,如果不这样做的话,就不能看出遮住后面的效果,看下图更容易理解,左图为填充父布局的方式,右图为
自适应的方式
私人空间initPopupWindow () {//要在布局中显示的布局 contentView=LayoutInflater.from(这).inflate (R.layout.popup_layout, null,假);//实例化PopupWindow并设置宽高 popupWindow=new popupWindow (contentView LinearLayout.LayoutParams。MATCH_PARENT LinearLayout.LayoutParams.MATCH_PARENT); popupWindow。setBackgroundDrawable(新BitmapDrawable ());//点击外部消失,这里因为PopupWindow填充了整个窗口,所以这句代码就没用了 popupWindow.setOutsideTouchable(真正的);//设置可以点击 popupWindow.setTouchable(真正的);//进入退出的动画 popupWindow.setAnimationStyle (R.style.MyPopWindowAnim); } 私人空间showPopWindow () { 视图rootview=LayoutInflater.from (MainActivity.this) .inflate (R.layout.activity_main, null); popupWindow。showAtLocation (rootview重力。底,0,0); }
Android底部弹窗的实现示例代码