我有一个自定义的ListView。在自定义ListView的布局中,我有一个ImageButton,它充当溢出菜单(类似于ActionBar上菜单的工作方式):
布局/item_list.xml
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_more_vert_black_24dp"
android:contentDescription="@string/descr_overflow_button"
android:id="@+id/overflowMenu"
android:layout_alignParentRight="true"/>
在本活动中,我在onCreate方法中将此ImageButton配置为弹出窗口:
overflowMenu = (ImageButton) findViewById(R.id.overflowMenu);
popupMenu = new PopupMenu(this, overflowMenu);
popupMenu.setOnMenuItemClickListener(this);
我还在onCreateOptionsMenu中对其进行了膨胀:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = popupMenu.getMenuInflater();
inflater.inflate(R.menu.popup_menu, popupMenu.getMenu());
return true;
}
我在menu/popup_menu.xml文件夹中有以下内容:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="@+id/report" android:title="Report"
app:showAsAction="always"/>
<item android:id="@+id/share" android:title="Share"
app:showAsAction="always"/>
</menu>
我加了这个:
@Override
public boolean onMenuItemClick(MenuItem item) {
return true;
}
但是,当我尝试单击电话上的ImageButton时,什么也没有发生。它不会按其应有的方式显示菜单项。我遗漏了什么?
发布于 2017-05-26 23:14:22
这个问题是重复的。对于自定义布局,您不能使用菜单,必须使用PopupWindow
Android MenuItem Custom Layout
PopupWindow popupwindow_obj = popupDisplay();
popupwindow_obj.showAsDropDown(clickbtn, -40, 18); // where u want show on view click event popupwindow.showAsDropDown(view, x, y);
public PopupWindow popupDisplay()
{
final PopupWindow popupWindow = new PopupWindow(this);
// inflate your layout or dynamically add view
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.mylayout, null);
Button item = (Button) view.findViewById(R.id.button1);
popupWindow.setFocusable(true);
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(view);
return popupWindow;
}
//在res/layout文件夹中创建名为my layout.xml的XML文件
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Window test" />
</LinearLayout>
发布于 2017-05-27 10:06:28
首先,PopupMenu
正是您所需要的。从docs
弹出菜单显示垂直列表中的项目列表,该列表锚定到调用菜单的视图。它非常适合于提供与特定内容相关的操作溢出,或者为命令的第二部分提供选项。
现在你的实现不能工作的原因是因为onCreateOptionsMenu
和onMenuItemClick
是用来管理activity的菜单的,所以膨胀溢出菜单是没有意义的。
imageButton = findViewById(R.id.overflow_menu);
PopupMenu popup = new PopupMenu(this, imageButton);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.popup_menu, popup.getMenu());
imageButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
popup.show();
}
});
你也可以看看上面链接的文档中的例子。
https://stackoverflow.com/questions/44211163
复制相似问题