如何将popup菜单代码replace为列表popupwindow?

目前,我正在编程创build一个popupmenu显示一个楼层和标题的列表。 但是,改变标题的背景颜色,并添加一个closuresbutton标题,结果是一场噩梦。

我想用一个列表popup窗口replace这个popupmenu,所以我可以添加一个带有背景属性的XML文件,用黑色作为背景的标题,菜单中项目的右侧和白色背景上的closuresbutton。 有一种方法,我可以实现这个列表popup窗口? 这是我的代码:

private void floorMenu(ImageView btnFloorMenu){ MapData data = new MapDao(MyPlugin.mapId); final List<Floor> flList = dao.getFloors(); // set popupMenu final PopupMenu floorsPm = new PopupMenu(MapViewActivity.this,btnFloorMenu); MenuItem titleItem = floorsPm.getMenu().add(Menu.NONE, Menu.NONE, Menu.NONE, "Floors"); int i = 1; for(Floor fl : flList) { floorsPm.getMenu().add(Menu.NONE, i,i, fl.getName()); if(i>3) break; i++; } // add popup listener floorsPm.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { // onClick @Override public boolean onMenuItemClick(MenuItem item){ // get floorname int flOrder = item.getOrder(); if(flOrder == Menu.NONE ) return true; flOrder--; final String floorId = flList.get(flOrder).getMapId(); // set camera to floor runOnUiThread(new Runnable() { @Override public void run() { floorsPm.dismiss(); mapFragment.getMapManager().setCameraLayer(floorId, false); Log.d(TAG, "post cameraLayer set"); changedSteps = true; pauseNav(); } }); return true; } }); floorsPm.show(); } 

Solutions Collecting From Web of "如何将popup菜单代码replace为列表popupwindow?"

请尝试这个代码,也许你不会喜欢这个

 private void floorMenu(ImageView btnFloorMenu){ final Dialog customDialog = new Dialog(this); customDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); customDialog.setContentView(R.layout.item_dialog_coustom_design); TextView clickItem = (TextView)customDialog.findViewById(R.id.item_click); TextView clickItem1 = (TextView)customDialog.findViewById(R.id.item_click1); TextView clickItem2 = (TextView)customDialog.findViewById(R.id.item_click2); Button btnClose = (Button)customDialog.findViewById(R.id.btn_close); clickItem.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { customDialog.dismiss(); // wright your Button Action } }); clickItem1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { customDialog.dismiss(); // wright your Button Action } }); clickItem2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { customDialog.dismiss(); // wright your Button Action } }); btnClose.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { customDialog.dismiss(); } }); customDialog.show(); } 

创buildLinearlayout layout_width =“280dp”layout_height =“wrap_content”android:orientation =“vertical”文件名item_dialog_coustom_design.xml然后把这个代码

  <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Your Title" android:background="#000" android:textColor="#fff" android:padding="12dp" android:textSize="20sp"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="1dp" android:background="#fff" android:padding="10dp" android:text="Your Item" android:id="@+id/item_click" android:textSize="16sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="1dp" android:background="#fff" android:padding="10dp" android:text="Your Item" android:id="@+id/item_click1" android:textSize="16sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="1dp" android:background="#fff" android:padding="10dp" android:text="Your Item" android:id="@+id/item_click2" android:textSize="16sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="10dp" android:paddingRight="10dp" android:paddingBottom="10dp" android:background="#fff" android:gravity="right"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn_close" android:text="Close"/> </LinearLayout> 

这里是我创build一个ListPopupWindow

首先 ,为item_list_popup_window的每个项目创build布局ListPopupWindow

 <?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:background="#e4e4e4" android:paddingTop="1dp" android:orientation="horizontal"> <TextView android:id="@+id/text_title" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" /> <Button android:id="@+id/button_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Delete" /> </LinearLayout> 

其次,为你的ListPopupWindow创build一个Adapter

 public class ListPopupWindowAdapter extends BaseAdapter{ private Activity mActivity; private List<String> mDataSource = new ArrayList<>(); private LayoutInflater layoutInflater; private OnClickDeleteButtonListener clickDeleteButtonListener; ListPopupWindowAdapter(Activity activity, List<String> dataSource, @NonNull OnClickDeleteButtonListener clickDeleteButtonListener){ this.mActivity = activity; this.mDataSource = dataSource; layoutInflater = mActivity.getLayoutInflater(); this.clickDeleteButtonListener = clickDeleteButtonListener; } @Override public int getCount() { return mDataSource.size(); } @Override public String getItem(int position) { return mDataSource.get(position); } @Override public long getItemId(int position) { return 0; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder; if(convertView == null){ holder = new ViewHolder(); convertView = layoutInflater.inflate(R.layout.item_list_popup_window, null); holder.tvTitle = (TextView) convertView.findViewById(R.id.text_title); holder.btnDelete = (Button) convertView.findViewById(R.id.button_delete); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } // bind data holder.tvTitle.setText(getItem(position)); holder.btnDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { clickDeleteButtonListener.onClickDeleteButton(position); } }); return convertView; } public class ViewHolder{ private TextView tvTitle; private Button btnDelete; } // interface to return callback to activity public interface OnClickDeleteButtonListener{ void onClickDeleteButton(int position); } } 

第三,你创build一个函数来创build并显示ListPopupWindow

 private void showListPopupWindow(View anchorView) { final ListPopupWindow listPopupWindow = new ListPopupWindow(this); listPopupWindow.setWidth(600); List<String> sampleData = new ArrayList<>(); sampleData.add("A"); sampleData.add("B"); sampleData.add("CCCCCCCCCCCCCC"); sampleData.add("D"); sampleData.add("EEEEEEEEE"); listPopupWindow.setAnchorView(anchorView); ListPopupWindowAdapter listPopupWindowAdapter = new ListPopupWindowAdapter(this, sampleData, new ListPopupWindowAdapter.OnClickDeleteButtonListener() { @Override public void onClickDeleteButton(int position) { Toast.makeText(MainActivity.this, "Click delete " + position, Toast.LENGTH_SHORT).show(); listPopupWindow.dismiss(); } }); listPopupWindow.setAdapter(listPopupWindowAdapter); listPopupWindow.show(); } 

最后,你可以通过显示ListPopupWindow

 showListPopupWindow(v); 

例如,如果你想单击button时显示它

 anyButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showListPopupWindow(v); } }); 

在这里输入图像说明

充分的演示在这里