Android PopupWindow实现左侧弹窗效果
程序员文章站
2023-11-28 21:15:10
本文实例为大家分享了android popupwindow实现左侧弹窗的具体代码,供大家参考,具体内容如下
效果图:
mainactivity.jav...
本文实例为大家分享了android popupwindow实现左侧弹窗的具体代码,供大家参考,具体内容如下
效果图:
mainactivity.java页面核心代码:
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //在setcontentview之前添加,未添加的话home键监听无效,设置窗体属性 this.getwindow().setflags(0x80000000, 0x80000000); setcontentview(r.layout.activity_main); //创建广播 //innerrecevier innerreceiver = new innerrecevier(); //动态注册广播 //intentfilter intentfilter = new intentfilter(intent.action_close_system_dialogs); //启动广播 //registerreceiver(innerreceiver, intentfilter); //外部网页 // init(); //pop button pop = (button) findviewbyid(r.id.popbutton); pop.setonclicklistener(popclick); } view.onclicklistener popclick = new view.onclicklistener() { @override public void onclick(view v) { getpopupwindow(); popupwindow.showatlocation(v, gravity.left,0,0); } }; /*创建popupwindow*/ protected void initpopupwindow(){ //获取自定义布局文件activity_pop_left.xml 布局文件 final view popipwindow_view = getlayoutinflater().inflate(r.layout.activity_pop_left,null,false); //创建popupwindow 实例,200,layoutparams.match_parent 分别是宽高 popupwindow = new popupwindow(popipwindow_view,300, viewgroup.layoutparams.match_parent,true); //设置动画效果 popupwindow.setanimationstyle(r.style.animationfade); //点击其他地方消失 popipwindow_view.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { if (popipwindow_view != null && popipwindow_view.isshown()) { popupwindow.dismiss(); popupwindow = null; } return false; } }); popupwindow.setbackgrounddrawable(new colordrawable(0)); button button1 = (button) popipwindow_view.findviewbyid(r.id.button1); button1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(getapplicationcontext(),"全屏显示",toast.length_short).show(); } }); } /*获取popipwinsow实例*/ private void getpopupwindow(){ if (null!=popupwindow){ popupwindow.dismiss(); return; }else { initpopupwindow(); } }
activity_main.xml页面
<framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.x.mainactivity" tools:ignore="mergerootframe" > <webview android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/webview" /> <button android:id="@+id/popbutton" android:text="点击弹出左菜单" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </framelayout>
左侧菜单需单独设置一个xml页面,style样式自定义。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。