Android实现底部弹窗效果
程序员文章站
2023-11-30 23:52:04
本文实例为大家分享了android实现底部弹窗效果的具体代码,供大家参考,具体内容如下
源代码地址:https://github.com/luoye123/box...
本文实例为大家分享了android实现底部弹窗效果的具体代码,供大家参考,具体内容如下
源代码地址:https://github.com/luoye123/box
东西很简单,我就直接亮代码了:
1、activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:id="@+id/ll_image"> <button android:id="@+id/bt_select_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选择图片" /> </linearlayout>
2、mainactivity.java
public class mainactivity extends appcompatactivity implements view.onclicklistener { private selectpicpopupwindow menuwindow; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); findviewbyid(r.id.bt_select_image).setonclicklistener(this); } @override public void onclick(view view) { switch (view.getid()) { case r.id.bt_select_image: //todo implement selectimgs(); } } private void selectimgs(){ menuwindow = new selectpicpopupwindow(mainactivity.this, itemsonclick); //设置弹窗位置 menuwindow.showatlocation(mainactivity.this.findviewbyid(r.id.ll_image), gravity.bottom | gravity.center_horizontal, 0, 0); } private view.onclicklistener itemsonclick = new view.onclicklistener() { public void onclick(view v) { menuwindow.dismiss(); switch (v.getid()) { case r.id.item_popupwindows_camera: //点击拍照按钮 break; case r.id.item_popupwindows_photo: //点击从相册中选择按钮 break; default: break; } } }; }
3、关键代码:selectpicpopupwindow.java
**public class selectpicpopupwindow extends popupwindow { private button item_popupwindows_camera, //弹窗拍照按钮 item_popupwindows_photo, //弹窗从相册选择按钮 item_popupwindows_cancel; //弹窗取消按钮 private view menuview; /** * 上传图片************************* * @param context * @param itemsonclick */ public selectpicpopupwindow(activity context, view.onclicklistener itemsonclick){ super(context); layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); menuview = inflater.inflate(r.layout.item_popupwindows,null); item_popupwindows_camera = (button) menuview.findviewbyid(r.id.item_popupwindows_camera); //拍照按钮 item_popupwindows_cancel = (button) menuview.findviewbyid(r.id.item_popupwindows_cancel); //取消按钮 item_**popupwindows_photo = (button) menuview.findviewbyid(r.id.item_popupwindows_photo); //图库按钮 /** * 取消按钮销毁事件 */ item_popupwindows_cancel.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { dismiss(); } }); item_popupwindows_camera.setonclicklistener(itemsonclick); item_popupwindows_photo.setonclicklistener(itemsonclick); //设置selectpicpopupwindow的view this.setcontentview(menuview); //设置selectpicpopupwindow**弹出窗体的宽 this.setwidth(viewgroup.layoutparams.fill_parent); //设置selectpicpopupwindow弹出窗体的高 //修改高度显示,解决被手机底部虚拟键挡住的问题 by黄海杰 at:2015-4-30 this.setheight(viewgroup.layoutparams.match_parent); //设置selectpicpopupwindow弹出窗体可点击 this.setfocusable(true); //设置selectpicpopupwindow弹出窗体动画效果 //this.setanimationstyle(r.style); //实例化一个colordrawable颜色为半透明 colordrawable dw = new colordrawable(0xb0000000); //设置selectpicpopupwindow弹出窗体的背景 this.setbackgrounddrawable(dw); //menuview添加ontouchlistener监听判断获取触屏位置如果在选择框外面则销毁弹出框 menuview.setontouchlistener(new view.ontouchlistener() { public boolean ontouch(view view, motionevent motionevent) { int height = menuview.findviewbyid(r.id.ll_popup).gettop(); int y = (int) motionevent.gety(); if (motionevent.getaction() == motionevent.action_up){ if (y<height){ dismiss(); } } return true; } }); } }**
写的不好,请见谅,,下一期完成后期的工作!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。