Android开发Popwindow仿微信右上角下拉菜单实例代码

Android开发Popwindow仿微信右上角下拉菜单实例代码,第1张

概述先给大家看下效果图:MenuPopwindow:packagecom.cloudeye.android.cloudeye.view;importandroid.app.Activity;

先给大家看下效果图:

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的VIEwthis.setContentVIEw(conentVIEw);// 设置SelectPicPopupWindowd出窗体的宽this.setWIDth(w / 3-30);// 设置SelectPicPopupWindowd出窗体的高this.setHeight(VIEwGroup.LayoutParams.WRAP_CONTENT);// 设置SelectPicPopupWindowd出窗体可点击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);// 设置SelectPicPopupWindowd出窗体动画效果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;}@OverrIDepublic int getCount() {return List == null ? 0 : List.size();}@OverrIDepublic Object getItem(int position) {return List.get(position);}@OverrIDepublic long getItemID(int position) {return position;}@OverrIDepublic 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()) {// 以下拉方式显示popupwindowthis.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"><ListVIEwandroID: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"><ImageVIEwandroID: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" /><TextVIEwandroID: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仿微信右上角下拉菜单实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的

总结

以上是内存溢出为你收集整理的Android开发Popwindow仿微信右上角下拉菜单实例代码全部内容,希望文章能够帮你解决Android开发Popwindow仿微信右上角下拉菜单实例代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1147814.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存