Android 按下ImageButton时不显示菜单项
我有一个自定义列表视图。在自定义ListView的布局中,我有一个ImageButton,它充当溢出菜单(类似于ActionBar上的菜单的工作方式): 布局/item_list.xmlAndroid 按下ImageButton时不显示菜单项,android,Android,我有一个自定义列表视图。在自定义ListView的布局中,我有一个ImageButton,它充当溢出菜单(类似于ActionBar上的菜单的工作方式): 布局/item_list.xml <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_more_vert_black_2
<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"/>
我还在OnCreateOptions菜单中对其进行了充气:
@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>
然而,当我试图点击手机上的ImageButton时,什么也没发生。它不会按应有的方式显示菜单项。我遗漏了什么?这个问题是重复。 对于不能使用菜单的自定义布局,必须使用弹出窗口 //在名为my layout.XML的res/layout文件夹中创建此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>
首先
弹出式菜单
正是您所需要的。从
一个弹出菜单显示一个垂直列表中的项目列表
锚定到调用菜单的视图。这有助于提供一个
与特定内容相关的操作溢出或提供
命令第二部分的选项
现在,您的实现不起作用的原因是因为onCreateOptions菜单
和onMenuItemClick
用于管理活动的菜单,因此在那里膨胀溢出菜单是没有意义的
您需要做的是将一个onClickListener
附加到ImageButton
上,初始化PopupMenu
并在ListView/RecyclerView适配器内调用show()
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();
}
});
您也可以查看上面链接的文档中的示例。您确定这一点吗?那弹出菜单的目的是什么?我一点也不懂。那为什么弹出菜单会存在呢?如果您不能在自定义版面中使用它,并且只能在主版面中使用它,那么为什么不在主版面中使用ActionBar呢?我看不出弹出菜单有什么用。为什么我要把mylayout.xml放在它的on布局文件中?请记住,此弹出菜单是为ListView中的每个列表项呈现的。如果这只是一个基本弹出窗口,为什么不使用DialogueBuilder?
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;
}
<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>
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();
}
});