首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >按下ImageButton时,它不显示菜单项

按下ImageButton时,它不显示菜单项
EN

Stack Overflow用户
提问于 2017-05-27 06:57:36
回答 2查看 319关注 0票数 0

我有一个自定义的ListView。在自定义ListView的布局中,我有一个ImageButton,它充当溢出菜单(类似于ActionBar上菜单的工作方式):

布局/item_list.xml

代码语言:javascript
运行
复制
<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配置为弹出窗口:

代码语言:javascript
运行
复制
    overflowMenu = (ImageButton) findViewById(R.id.overflowMenu);
    popupMenu = new PopupMenu(this, overflowMenu);
    popupMenu.setOnMenuItemClickListener(this);

我还在onCreateOptionsMenu中对其进行了膨胀:

代码语言:javascript
运行
复制
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = popupMenu.getMenuInflater();
    inflater.inflate(R.menu.popup_menu, popupMenu.getMenu());
    return true;
}

我在menu/popup_menu.xml文件夹中有以下内容:

代码语言:javascript
运行
复制
<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>

我加了这个:

代码语言:javascript
运行
复制
@Override
public boolean onMenuItemClick(MenuItem item) {
    return true;
}

但是,当我尝试单击电话上的ImageButton时,什么也没有发生。它不会按其应有的方式显示菜单项。我遗漏了什么?

EN

回答 2

Stack Overflow用户

发布于 2017-05-27 07:14:22

这个问题是重复的。对于自定义布局,您不能使用菜单,必须使用PopupWindow

Android MenuItem Custom Layout

代码语言:javascript
运行
复制
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文件

代码语言:javascript
运行
复制
<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>
票数 0
EN

Stack Overflow用户

发布于 2017-05-27 18:06:28

首先,PopupMenu正是您所需要的。从docs

弹出菜单显示垂直列表中的项目列表,该列表锚定到调用菜单的视图。它非常适合于提供与特定内容相关的操作溢出,或者为命令的第二部分提供选项。

现在你的实现不能工作的原因是因为onCreateOptionsMenuonMenuItemClick是用来管理activity的菜单的,所以膨胀溢出菜单是没有意义的。

代码语言:javascript
运行
复制
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();
    }    
});

你也可以看看上面链接的文档中的例子。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44211163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档