English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Let's take a look at the effect diagram first:
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 Yuan Lei 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(); // Set the View of the SelectPicPopupWindow this.setContentView(conentView); // Set the width of the SelectPicPopupWindow pop-up window this.setWidth(w / 3-30); // Set the height of the SelectPicPopupWindow pop-up window this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); // Set the SelectPicPopupWindow pop-up window to be clickable this.setFocusable(true); this.setOutsideTouchable(true); // Refresh the status this.update(); // Create a ColorDrawable with semi-transparent color ColorDrawable dw = new ColorDrawable(0000000000); // Press the back key and other places to make it disappear, set this to trigger OnDismisslistener, set other control changes, and perform other operations this.setBackgroundDrawable(dw); // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog); // Set the animation effect of the SelectPicPopupWindow pop-up window 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();63; 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; } } /** * Display popupWindow * * @param parent */ public void showPopupWindow(View parent) { if (!this.isShowing()) { // Display popupwindow in a dropdown manner this.showAsDropDown(parent); } else { this.dismiss(); } } }
MenuPopwindow layout:
<?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 layout file:
<?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="Test" android:textColor="@color/black" android:textSize="18sp" /> </LinearLayout>
Usage:
int[] icons = {R.mipmap.icon_menu_item_edit, R.mipmap.icon_menu_item_delete}; String[] texts = {"Edit", "Delete"}; 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));//Click the button on the upper right corner
The following is the Android development Popwindow仿WeChat upper right corner dropdown menu example code introduced by the editor for everyone, hoping it will be helpful to everyone. If you have any questions, please leave me a message, and the editor will reply to everyone in time.