我是
Android新手,我正在创建Listview弹出菜单.但我有它的宽度和高度问题.弹出菜单可以采用更高的高度和宽度. SO中有很多问题,但这些都没有帮助我.
private void showPopupMenu(View view){ Context wrapper = new ContextThemeWrapper(this,R.style.PopupMenu); PopupMenu popupMenu = new PopupMenu(wrapper,view); popupMenu.getMenuInflater().inflate(R.menu.popup_menu,popupMenu.getMenu()); popupMenu.show(); popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){ @Override public boolean onMenuItemClick(MenuItem item){ switch (item.getItemId()){ case R.id.install: Intent intent = new Intent(ViewAllRelationActivity.this,EditRelativeActivity.class); startActivity(intent); break; case R.id.addtowishlist: break; } return false; } }); }
它给出了这个输出:
我们可以在ContextMenu中保持宽度和高度但是它总是在Center中显示而不是Listview数据的每一行.
但我想要下面的图像类型弹出菜单.宽度和高度都很小.
请为此提供解决方案.
解决方法
如果您使用的是适配器,则可以在适配器中放入getView(…)方法
imvMore.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showPopupMenu(act,v); } });
并把方法
private void showPopupMenu(Activity act,View view){ PopupMenu popupMenu = new PopupMenu(act,view); popupMenu.getMenuInflater().inflate(R.menu.menu_popup,popupMenu.getMenu()); popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { } return true; } }); popupMenu.show(); }
在您的适配器类中.
注意:act是在创建构造函数适配器时必须绑定的Activity,例如:
public YourAdapter(Activity act,ArrayList<ItemOfYourModel> data){ this.data = data; this.act = act; }
在Activity中,您可以编写代码:
ArrayList<ItemOfYourModel> listData = new ArrayList<ItemOfYourModel>(); listData.add(new YourItemOfYourModel(...)); YourAdapter adapter = new YourAdapter(this,listData);