Android自定义PopupWindow简单小例子
程序员文章站
2024-02-28 09:43:04
最近没事做就写了一下popupwindow,希望对有些人有点帮助。
照常先看一下完成后的结果(界面比较难看就不要吐槽了)
点击地理位置然后弹出的popupwin...
最近没事做就写了一下popupwindow,希望对有些人有点帮助。
照常先看一下完成后的结果(界面比较难看就不要吐槽了)
点击地理位置然后弹出的popupwindow,数据我写死了但是可以根据你们的需求自己改,或者通过网络获取数据。我是通过listview进行展示的你们也可以改成表格布局,具体的实现代码如下:
popupwindow的弹出框的整体布局(listview)fragment_popup:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <listview android:id="@+id/pop_path" android:layout_width="match_parent" android:layout_height="wrap_content"> </listview> </linearlayout>
listview要加载的item:pop_list_adapter.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <textview android:id="@+id/item_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:textsize="18dp"/> </linearlayout>
listview的适配器:popadapter
public class popadapter extends baseadapter { private list<string> list; private context context; public popadapter(list<string> list, context context) { this.list = list; this.context = context; } @override public int getcount() { return list.size(); } @override public object getitem(int position) { return position; } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder viewholder; if (convertview == null) { viewholder = new viewholder(); convertview = layoutinflater.from(context).inflate(r.layout.pop_list_adapter, null); viewholder.item_content = (textview) convertview.findviewbyid(r.id.item_content); convertview.settag(viewholder); } else { viewholder = (viewholder) convertview.gettag(); } viewholder.item_content.settext(list.get(position)); return convertview; } private class viewholder { private textview item_content; } }
写一个mypopupwindow类继承popupwindow:
public class mypopuwindow extends popupwindow { private view contentview; private listview lv_pop; private list<string> paths; private context context; public mypopuwindow(final activity context) { this.context = context; //获得 layoutinflater 的实例 layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); contentview = inflater.inflate(r.layout.fragment_popup, null); //获取屏幕的宽高 int h = context.getwindowmanager().getdefaultdisplay().getheight(); int w = context.getwindowmanager().getdefaultdisplay().getwidth(); this.setcontentview(contentview); // 设置selectpicpopupwindow弹出窗体的宽 this.setwidth(layoutparams.match_parent); // 设置selectpicpopupwindow弹出窗体的高 this.setheight(layoutparams.wrap_content); // 设置selectpicpopupwindow弹出窗体可点击 this.setfocusable(true); this.setoutsidetouchable(true); // 刷新状态 this.update(); // 实例化一个colordrawable颜色为半透明 colordrawable dw = new colordrawable(0000000000); // 点back键和其他地方使其消失,设置了这个才能触发ondismisslistener ,设置其他控件变化等操作 this.setbackgrounddrawable(dw); // 设置selectpicpopupwindow弹出窗体动画效果 this.setanimationstyle(r.style.animationpreview); initdata(); } private void initdata() { paths = new arraylist<>(); paths.add("北京"); paths.add("上海"); paths.add("广州"); paths.add("天津"); paths.add("大连"); paths.add("长春"); paths.add("济南"); paths.add("青岛"); paths.add("无锡"); paths.add("郑州"); paths.add("宁波"); paths.add("厦门"); lv_pop = (listview) contentview.findviewbyid(r.id.pop_path); lv_pop.setadapter(new popadapter(paths, context)); lv_pop.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { toast.maketext(context, paths.get(position), toast.length_short).show(); showpopupwindow(view); } }); } /** * 显示popupwindow * * @param parent */ public void showpopupwindow(view parent) { if (!this.isshowing()) { // 以下拉方式显示popupwindow this.showasdropdown(parent, parent.getlayoutparams().width / 2, 18); } else { this.dismiss(); } } }
接下来就是调用popupwindow显示了。actionpath:是你的组件也就是我的地理位置
mypopuwindow= new mypopuwindow(getactivity()); mypopuwindow.showpopupwindow(actionpath);
好了大概的一个代码就是这样了希望对你们有用。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。