Android 使用PopupWindow实现弹出更多的菜单实例详解

最近想要做一个弹出更多的菜单,而原生的弹出菜单却不是我们想要的效果,所以必然要自定义菜单咯。本人也是借鉴网上的资料进行封装的,感觉还蛮不错的。

原生的菜单如下图:

自定义之后的效果图:

是不是看到这里之后,对比可知,原生的效果不太理想,所以还是再自己定义吧!

1、PopupWindow可以说是一个浮动在Activity之上的容器,通常用来显示自定义的视图。弹出菜单的封装PopMenuMore

/**
 * 对弹出菜单的封装.
 * http://blog.csdn.net/maosidiaoxian/article/details/39178167
 * Author: msdx (645079761@qq.com)
 * Time: 14-6-13 下午1:51
 */
public class PopMenuMore {
 /**
 * 上下文.
 */
 private Context mContext;
 /**
 * 菜单项
 */
 private ArrayList<PopMenuMoreItem> mItemList;
 /**
 * 列表适配器.
 */
 private BaseAdapter mAdapter;
 /**
 * 菜单选择监听.
 */
 private OnItemSelectedListener mListener;
 /**
 * 下角图标
 */
 private ImageView cornerIcon;
 /**
 * 列表.
 */
 private ListView mListView;
 /**
 * 弹出窗口.
 */
 private PopupWindow mPopupWindow;
 public PopMenuMore(Context context) {
 mContext = context;
 mItemList = new ArrayList<>();
 View view = onCreateView(context);
 view.setFocusableInTouchMode(true);
 mAdapter = onCreateAdapter(context, mItemList);
 cornerIcon = findCornerView(view);
 mListView = findListView(view);
 mListView.setAdapter(mAdapter);
 mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  PopMenuMoreItem item = (PopMenuMoreItem) mAdapter.getItem(position);
  if (mListener != null) {
   mListener.selected(view, item, position);
  }
  mPopupWindow.dismiss();
  }
 });
 view.setOnKeyListener(new View.OnKeyListener() {
  @Override
  public boolean onKey(View v, int keyCode, KeyEvent event) {
  if (keyCode == KeyEvent.KEYCODE_MENU && mPopupWindow.isShowing()) {
   mPopupWindow.dismiss();
   return true;
  }
  return false;
  }
 });
 mPopupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
 mPopupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
 setBackgroundColor(Color.parseColor("#000000"));
 setCorner(R.mipmap.triangle);
 }
 /**
 * 设置ListView背景
 *
 * @param argb Color.parseColor("..")
 */
 public void setBackgroundColor(int argb) {
// int strokeWidth = 5; // 3dp 边框宽度
 int roundRadius = 5; // 8dp 圆角半径
// int strokeColor = Color.parseColor("#2E3135");//边框颜色
// int fillColor = Color.parseColor("#DFDFE0");//内部填充颜色
 GradientDrawable gd = new GradientDrawable();//创建drawable
 gd.setColor(argb);
 gd.setCornerRadius(roundRadius);
// gd.setStroke(strokeWidth, strokeColor);
 mListView.setBackgroundDrawable(gd);
 }
 /**
 * 设置下角图标
 *
 * @param resId
 */
 public void setCorner(int resId) {
 cornerIcon.setBackgroundResource(resId);
 }
 protected View onCreateView(Context context) {
 return LayoutInflater.from(context).inflate(R.layout.layout_popmenu_more, null);
 }
 protected ImageView findCornerView(View view) {
 return (ImageView) view.findViewById(R.id.corner_iv);
 }
 protected ListView findListView(View view) {
 return (ListView) view.findViewById(R.id.menu_listview);
 }
 /**
 * 菜单列表中的适配器.
 *
 * @param context
 * @param items 表示所有菜单项.
 * @return
 */
 protected BaseAdapter onCreateAdapter(Context context, ArrayList<PopMenuMoreItem> items) {
 return new PopMenuMoreAdapter(context, items);
 }
 /**
 * 添加菜单项
 *
 * @param item
 */
 public void addItem(PopMenuMoreItem item) {
 mItemList.add(item);
 mAdapter.notifyDataSetChanged();
 }
 public void addItems(List<PopMenuMoreItem> items) {
 if (items != null) {
  mItemList.clear();
 }
 for (PopMenuMoreItem item : items) {
  mItemList.add(item);
 }
 mAdapter.notifyDataSetChanged();
 } 

 /**
 * 作为指定View的下拉控制显示.
 *
 * @param parent 所指定的View
 */
 public void showAsDropDown(View parent) {
 mPopupWindow.showAsDropDown(parent);
 }
 /**
 * 隐藏菜单.
 */
 public void dismiss() {
 mPopupWindow.dismiss();
 }
 /**
 * 设置菜单选择监听.
 *
 * @param listener 监听器.
 */
 public void setOnItemSelectedListener(OnItemSelectedListener listener) {
 mListener = listener;
 }
 /**
 * 当前菜单是否正在显示.
 *
 * @return
 */
 public boolean isShowing() {
 return mPopupWindow.isShowing();
 }
 /**
 * 菜单项选择监听接口.
 */
 public interface OnItemSelectedListener {
 /**
  * 菜单被选择时的回调接口.
  *
  * @param view 被选择的内容的View.
  * @param item 被选择的菜单项.
  * @param position 被选择的位置.
  */
 void selected(View view, PopMenuMoreItem item, int position);
 }
} 

2、菜单中ListView的适配器:PopMenuMoreAdapter

/**
 * @author SoBan
 * @create 2017/4/12 10:29.
 */
public class PopMenuMoreAdapter extends BaseAdapter {
 private ArrayList<PopMenuMoreItem> items;
 private Context context;
 public PopMenuMoreAdapter(Context context, ArrayList<PopMenuMoreItem> items) {
 this.context = context;
 this.items = items;
 }
 @Override
 public int getCount() {
 return items.size();
 }
 @Override
 public PopMenuMoreItem getItem(int position) {
 return items.get(position);
 }
 @Override
 public long getItemId(int position) {
 return position;
 }
 @Override
 public View getView(int position, View view, ViewGroup parent) {
 if (view == null) {
  view = LayoutInflater.from(context).inflate(R.layout.item_popmenu_more, null);
  ViewHolder holder = new ViewHolder();
  holder.icon = (ImageView) view.findViewById(R.id.menu_icon);
  holder.text = (TextView) view.findViewById(R.id.menu_text);
  view.setTag(holder);
 } else if (view.getParent() != null) {
  ((ViewGroup) view.getParent()).removeView(view);
 }
 ViewHolder holder = (ViewHolder) view.getTag();
 PopMenuMoreItem item = items.get(position);
 if (item.getResId() == 0) {
  holder.icon.setVisibility(View.GONE);
 }
 holder.text.setText(item.getText());
 return view;
 }
 private class ViewHolder {
 ImageView icon;
 TextView text;
 }
} 

3、菜单项中item:  PopMenuMoreItem

/**
 * 菜单项.
 */
public class PopMenuMoreItem {
 public int id; //标识
 public int resId; //资源图标
 public String text;//文字
 public PopMenuMoreItem(int id, String text) {
 this.id = id;
 this.resId = 0;
 this.text = text;
 }
 public PopMenuMoreItem(int id, int resId, String text) {
 this.id = id;
 this.resId = resId;
 this.text = text;
 }
 public int getId() {
 return id;
 }
 public void setId(int id) {
 this.id = id;
 }
 public int getResId() {
 return resId;
 }
 public void setResId(int resId) {
 this.resId = resId;
 }
 public String getText() {
 return text;
 }
 public void setText(String text) {
 this.text = text;
 }
} 

4、宽度适配内容、不滚动的ListView:PopMenuMoreListView

/**
 * 宽度适配内容的ListView.
 * Author: msdx (645079761@qq.com)
 * Time: 14-9-2 下午5:14
 */
public class PopMenuMoreListView extends ListView {
 public PopMenuMoreListView(Context context) {
 super(context);
 }
 public PopMenuMoreListView(Context context, AttributeSet attrs) {
 super(context, attrs);
 }
 public PopMenuMoreListView(Context context, AttributeSet attrs, int defStyle) {
 super(context, attrs, defStyle);
 }
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 int width = 0;
 for (int i = 0; i < getChildCount(); i++) {
  View child = getChildAt(i);
  child.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), heightMeasureSpec);
  int w = child.getMeasuredWidth();
  if (w > width) width = w;
 }
 widthMeasureSpec = MeasureSpec.makeMeasureSpec(width + getPaddingLeft() + getPaddingRight(), MeasureSpec.EXACTLY);
 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 }
} 

5、item的布局:item_popmenu_more.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:paddingBottom="10dip"
 android:paddingLeft="20dip"
 android:paddingRight="20dip"
 android:paddingTop="10dip">
 <ImageView
 android:id="@+id/menu_icon"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center_vertical"
 android:layout_marginLeft="5dip"
 android:layout_marginRight="5dip"
 android:src="@mipmap/demand_icon_location" />
 <TextView
 android:id="@+id/menu_text"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center_vertical"
 android:singleLine="true"
 android:textColor="#FFFFFF" />
</LinearLayout> 

6、更多菜单的布局:layout_popmenu_more.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="wrap_content"
 android:orientation="vertical"
 android:paddingRight="5dip">
 <ImageView
 android:id="@+id/corner_iv"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="right"
 android:layout_marginRight="15dip"
 android:contentDescription="@null" />
 <soban.orderscroll.PopMenuMoreListView
 android:id="@+id/menu_listview"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="right"
 android:cacheColorHint="@android:color/transparent"
 android:listSelector="@android:color/transparent"
 android:divider="#FFFFFF"
 android:dividerHeight="1px"
 android:focusable="true" />
</LinearLayout> 

7、例子Activity: MainActivity

public class MainActivity extends Activity {
 private static final int USER_SEARCH = 0;
 private static final int USER_ADD = 1;
 private PopMenuMore mMenu;
 private TextView mTextView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 initMenu();
 mTextView = (TextView) findViewById(R.id.hello_tv);
 mTextView.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
  mMenu.showAsDropDown(mTextView);
  }
 });
 }
 private void initMenu() {
 mMenu = new PopMenuMore(this);
 // mMenu.setCorner(R.mipmap.demand_icon_location);
 // mMenu.setBackgroundColor(Color.parseColor("#ff8800"));
 ArrayList<PopMenuMoreItem> items = new ArrayList<>();
 items.add(new PopMenuMoreItem(USER_SEARCH, "搜索"));
 items.add(new PopMenuMoreItem(USER_ADD, "添加"));
 items.add(new PopMenuMoreItem(USER_SEARCH, "搜索"));
 items.add(new PopMenuMoreItem(USER_ADD, "添加"));
 items.add(new PopMenuMoreItem(USER_SEARCH, "搜索"));
 items.add(new PopMenuMoreItem(USER_ADD, "添加"));
 /*items.add(new PopMenuMoreItem(USER_SEARCH, R.mipmap.demand_icon_number, "搜索"));
 items.add(new PopMenuMoreItem(USER_ADD, R.mipmap.demand_icon_location, "添加"));
 items.add(new PopMenuMoreItem(USER_SEARCH, R.mipmap.demand_icon_number, "搜索"));
 items.add(new PopMenuMoreItem(USER_ADD, R.mipmap.demand_icon_location, "添加"));
 items.add(new PopMenuMoreItem(USER_SEARCH, R.mipmap.demand_icon_number, "搜索"));
 items.add(new PopMenuMoreItem(USER_ADD, R.mipmap.demand_icon_location, "添加"));*/
 mMenu.addItems(items);
 mMenu.setOnItemSelectedListener(new PopMenuMore.OnItemSelectedListener() {
  @Override
  public void selected(View view, PopMenuMoreItem item, int position) {
  switch (item.id) {
   case USER_SEARCH:
//   startActivity(new Intent(this, UserSearchActivity.class));
   break;
   case USER_ADD:
//   startActivity(new Intent(getActivity(), UserAddActivity.class));
   break;
  }
  }
 });
 }
} 

8、例子布局:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin">
 <TextView
 android:id="@+id/hello_tv"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Hello World!" />
</RelativeLayout>

9、所需资源文件:

参考:

android使用PopupWindow实现页面点击顶部弹出下拉菜单

以上所述是小编给大家介绍的Android 使用PopupWindow实现弹出更多的菜单实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

(0)

相关推荐

  • Android自定义弹出窗口PopupWindow使用技巧

    PopupWindow是Android上自定义弹出窗口,使用起来很方便. PopupWindow的构造函数为 复制代码 代码如下: public PopupWindow(View contentView, int width, int height, boolean focusable) contentView为要显示的view,width和height为宽和高,值为像素值,也可以是MATCHT_PARENT和WRAP_CONTENT. focusable为是否可以获得焦点,这是一个很重要的参数

  • Android中PopupWindow使用方法详解

    参考原文Android PopupWindow用法解析进行学习,通过实例及PopupWindow源码分析了PopupWindow的使用.文章最后的"补充Case: 弹窗不消失,但是事件向下传递"很赞. 不过,源码已经发生了变化,文章中提到的PopupViewContainer类,在目前的源码(Android6.0)中使用的是PopupBackgroundView和PopupDecorView共同完成的. 而在6.0版本的PopupWindow的preparePopup方法中,无论是否s

  • Android中使用PopupWindow 仿微信点赞和评论弹出

    微信朋友圈的点赞和评论功能,有2个组成部分:左下角的"更多"按钮:点击该按钮后弹出的对话框: PopupWindow,弹出框使用PopupWindow实现,这是点赞和评论的载体,具体要涉及 PopupWindow 点击非窗口位置和再次点击消失以及显示位置的问题(根据相应更多按钮的位置确定 PopupWindow 的显示位置 package com.example.cmm.helloworld; import android.app.AlertDialog; import android

  • Android PopupWindow使用方法小结

    前几天要用到PopupWindow,一时竟想不起来怎么用,赶紧上网查了查,自己写了个demo,并在此记录一下PopupWindow的用法. 使用场景 PopupWindow,顾名思义,就是弹窗,在很多场景下都可以见到它.例如ActionBar/Toolbar的选项弹窗,一组选项的容器,或者列表等集合的窗口等等. 基本用法 使用PopupWindow很简单,可以总结为三个步骤: 创建PopupWindow对象实例: 设置背景.注册事件监听器和添加动画: 显示PopupWindow. 其中,第二步是

  • Android Popupwindow弹出窗口的简单使用方法

    本文实例为大家分享了Android Popupwindow弹出窗口的具体代码,供大家参考,具体内容如下 代码很简单,没有和别的控件连用.布局自己随意定义,我的这个是最基础的,就直接上代码啦! 在MainActivity里 import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.LayoutInflat

  • Android popupwindow简单使用方法介绍

    先看下效果 1.首页 package com.yskj.jh.demopopupwindow; import android.content.Context; import android.graphics.drawable.BitmapDrawable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import and

  • PopupWindow使用方法详解

    学习了Android PopupWindow的使用技巧和[Android UI设计与开发]7.底部菜单栏(四)PopupWindow 实现显示仿腾讯新闻底部弹出菜单,然后自己进行了一下研究,写一个总结,方便以后学习. 效果图: 1.PopupWindow的布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.androi

  • Android组件popupwindow使用方法详解

    先看效果: 现在很多的应用效果都需要做的炫些,像UC,以及天天静听,效果很炫的,源码已经对外开放了,有兴趣的可以去研究下的 上源码 main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fi

  • Android PopupWindow使用实例

    示例效果如下:   MainActivity.xml package sn.qdj.popupwindowdemo; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widg

  • android使用PopupWindow实现页面点击顶部弹出下拉菜单

    实现此功能没有太多的技术难点,主要通过PopupWindow方法,同时更进一步加深了PopupWindow的使用,实现点击弹出一个自定义的view,view里面可以自由设计,比较常用的可以放一个listview. demo中我只是一个点击展示,简单的使用了fade in out的动画效果,也没有精美的图片资源,看着也丑,不过这么短的时间,让你掌握一个很好用的技术,可以自己扩展,不很好么? 废话不说了,直接上代码: MainActivity.java public class MainActivi

随机推荐