文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2022-06-06 01:05

关注

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

原生的菜单如下图:

自定义之后的效果图:

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

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


 
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); 
 } 
  
 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); 
 } 
  
 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); 
 } 
  
 protected BaseAdapter onCreateAdapter(Context context, ArrayList<PopMenuMoreItem> items) { 
 return new PopMenuMoreAdapter(context, items); 
 } 
  
 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(); 
 } 
  
 public void showAsDropDown(View parent) { 
 mPopupWindow.showAsDropDown(parent); 
 } 
  
 public void dismiss() { 
 mPopupWindow.dismiss(); 
 } 
  
 public void setOnItemSelectedListener(OnItemSelectedListener listener) { 
 mListener = listener; 
 } 
  
 public boolean isShowing() { 
 return mPopupWindow.isShowing(); 
 } 
  
 public interface OnItemSelectedListener { 
  
 void selected(View view, PopMenuMoreItem item, int position); 
 } 
} 

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


 
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


 
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, "添加")); 
  
 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实现弹出更多的菜单实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程网网站的支持!

您可能感兴趣的文章:Android PopupMenu弹出菜单的实现Android中PopupMenu组件的使用实例Android编程实现自定义PopupMenu样式示例【显示图标与设置RadioButton图标】Android之用PopupWindow实现弹出菜单的方法详解android使用PopupWindow实现页面点击顶部弹出下拉菜单android自定义popupwindow仿微信右上角弹出菜单效果Android PopupWindow实现右侧、左侧和底部弹出菜单Android使用Activity实现从底部弹出菜单或窗口的方法Android开发使用PopupMenu创建弹出式菜单完整实例


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯