Android - 按下列表项视图时的弹出菜单? [英] Android - Popup menu when list item view pressed?
问题描述
我想实现一个类似于谷歌游戏商店的弹出菜单,如下所示.
i would like to implement a popup menu similar to google's play store as shown below.
所以基本上根据我的理解,我需要一个活动和这个活动的布局,其中定义了一个列表视图.我需要创建我的自定义适配器.另外,我需要创建一个包含信息和视图(带有 3 个点)的列表布局,作为启动弹出菜单的按钮?我在这里看到的问题是,如何仅为该视图创建侦听器,以及如何在列表视图中引用该特定列表项的值.
so basically from what i understand, i'll need an activity and a layout for this activity with a listview defined in it. i need to create my custom adapter. also, i need to create a list layout would contain the information and a view (with the 3 dots) that will serve as the button to launch the popup menu? the issue that i'm seeing here is that how do i create a listener for this view only and how do i reference the value for that specific list item in the list view.
我还没有任何可用的代码,因为我还没有开始任何与此相关的事情.我目前正在获取理论上的信息,但如果需要,我将创建一个示例代码.
i don't have any code available yet as i haven't started anything related to this. i'm currently getting info in theory for now but if required i will create a sample code.
谢谢.
推荐答案
使用 popup菜单通过以下三个步骤创建菜单非常简单:
Using popup menu it's quite simple to create a menu with these three steps:
1 - 使用 OnClickListener
或我更喜欢从布局 xml 中将点击侦听器添加到菜单按钮:
1 - Add a click listener to the menu button using OnClickListener
or as i prefer from the layout xml:
<ImageButton android:id="@+id/menu_button" android:onClick="showMenu" ... />
2 - 创建菜单布局 menu_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/item_settings"
android:showAsAction="ifRoom|withText"
android:title="Settings"
android:visible="true"/>
<item
android:id="@+id/item_about"
android:showAsAction="ifRoom|withText"
android:title="About"
android:visible="true"/>
</menu>
3 - 创建一个弹出菜单,扩展 xml 布局并显示它:
3 - Create a popup menu, inflate the xml layout and show it:
public void showMenu (View view)
{
PopupMenu menu = new PopupMenu (this, view);
menu.setOnMenuItemClickListener (new PopupMenu.OnMenuItemClickListener ()
{
@Override
public boolean onMenuItemClick (MenuItem item)
{
int id = item.getItemId();
switch (id)
{
case R.id.item_settings: Log.i (Tag, "settings"); break;
case R.id.item_about: Log.i (Tag, "about"); break;
}
return true;
}
});
menu.inflate (R.menu.menu_layout);
menu.show();
}
这篇关于Android - 按下列表项视图时的弹出菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!