Android开发Popwindow仿微信右上角下拉菜单实例代码
程序员文章站
2024-03-02 20:02:40
先给大家看下效果图:
menupopwindow:
package com.cloudeye.android.cloudeye.view;
import...
先给大家看下效果图:
menupopwindow:
package com.cloudeye.android.cloudeye.view; import android.app.activity; import android.content.context; import android.graphics.drawable.colordrawable; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.baseadapter; import android.widget.imageview; import android.widget.listview; import android.widget.popupwindow; import android.widget.textview; import com.cloudeye.android.cloudeye.r; import com.cloudeye.android.cloudeye.base.menupopwindowbean; import java.util.list; /** * created by 袁磊 on 2016/10/26. */ public class menupopwindow extends popupwindow { private view conentview; private listview lvcontent; public menupopwindow(activity context, list<menupopwindowbean> list) { layoutinflater inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); conentview = inflater.inflate(r.layout.menu_popup_window, null); lvcontent = (listview) conentview.findviewbyid(r.id.lv_toptitle_menu); lvcontent.setadapter(new myadapter(context, list)); int h = context.getwindowmanager().getdefaultdisplay().getheight(); int w = context.getwindowmanager().getdefaultdisplay().getwidth(); // 设置selectpicpopupwindow的view this.setcontentview(conentview); // 设置selectpicpopupwindow弹出窗体的宽 this.setwidth(w / 3-30); // 设置selectpicpopupwindow弹出窗体的高 this.setheight(viewgroup.layoutparams.wrap_content); // 设置selectpicpopupwindow弹出窗体可点击 this.setfocusable(true); this.setoutsidetouchable(true); // 刷新状态 this.update(); // 实例化一个colordrawable颜色为半透明 colordrawable dw = new colordrawable(0000000000); // 点back键和其他地方使其消失,设置了这个才能触发ondismisslistener ,设置其他控件变化等操作 this.setbackgrounddrawable(dw); // mpopupwindow.setanimationstyle(android.r.style.animation_dialog); // 设置selectpicpopupwindow弹出窗体动画效果 this.setanimationstyle(r.style.animationpreview); } public void setonitemclick(adapterview.onitemclicklistener myonitemclicklistener) { lvcontent.setonitemclicklistener(myonitemclicklistener); } class myadapter extends baseadapter { private list<menupopwindowbean> list; private layoutinflater inflater; public myadapter(context context, list<menupopwindowbean> list) { inflater = layoutinflater.from(context); this.list = list; } @override public int getcount() { return list == null ? 0 : list.size(); } @override public object getitem(int position) { return list.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { holder holder = null; if (convertview == null) { convertview = inflater.inflate(r.layout.menu_popup_window_item, null); holder = new holder(); holder.ivitem = (imageview) convertview.findviewbyid(r.id.iv_menu_item); holder.tvitem = (textview) convertview.findviewbyid(r.id.tv_menu_item); convertview.settag(holder); } else { holder = (holder) convertview.gettag(); } holder.ivitem.setimageresource(list.get(position).geticon()); holder.tvitem.settext(list.get(position).gettext()); return convertview; } class holder { imageview ivitem; textview tvitem; } } /** * 显示popupwindow * * @param parent */ public void showpopupwindow(view parent) { if (!this.isshowing()) { // 以下拉方式显示popupwindow this.showasdropdown(parent); } else { this.dismiss(); } } }
menupopwindow布局:
<?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" android:orientation="vertical" android:paddingright="10dp"> <listview android:id="@+id/lv_toptitle_menu" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@mipmap/back_toptitle_menu" /> </linearlayout>
adapter中的item布局文件:
<?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" android:gravity="center" android:orientation="horizontal" android:paddingbottom="10dp" android:paddingtop="10dp"> <imageview android:id="@+id/iv_menu_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginright="10dp" android:src="@mipmap/icon_menu_item_edit" /> <textview android:id="@+id/tv_menu_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="测试" android:textcolor="@color/black" android:textsize="18sp" /> </linearlayout>
使用:
int[] icons = {r.mipmap.icon_menu_item_edit, r.mipmap.icon_menu_item_delete}; string[] texts = {"编辑", "删除"}; list<menupopwindowbean> list = new arraylist<>(); menupopwindowbean bean = null; for (int i = 0; i < icons.length; i++) { bean = new menupopwindowbean(); bean.seticon(icons[i]); bean.settext(texts[i]); list.add(bean); } menupopwindow pw = new menupopwindow(personalimgplayactivity.this, list); pw.setonitemclick(myonitemclicklistener); pw.showpopupwindow(findviewbyid(r.id.img_top1_share));//点击右上角的那个button
以上所述是小编给大家介绍的android开发popwindow仿微信右上角下拉菜单实例代码,希望对大家有所帮助