按下时,ImageButton不显示菜单项

时间:2017-05-26 22:57:36

标签: android

我有一个自定义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"/>

在Activity中,我在onCreate方法中将此ImageButton配置为Popup:

    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时,没有任何反应。它不会显示菜单项。我错过了什么?

2 个答案:

答案 0 :(得分:0)

这个问题是重复的。 对于自定义布局,您不能使用菜单,您必须使用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;
}

//在名为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>

答案 1 :(得分:0)

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

  

弹出菜单显示垂直列表中的项目列表   锚定到调用菜单的视图。它提供了一个很好的选择   溢出与特定内容或提供相关的操作   命令的第二部分的选项。

现在您的实施无效的原因是因为onCreateOptionsMenuonMenuItemClick用于管理活动的菜单,因此夸大溢出菜单毫无意义。

您需要做的是将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();
    }    
});

您也可以查看上面链接的文档以获取示例。