14

我想实现一个类似于谷歌游戏商店的弹出菜单,如下所示。

在此处输入图像描述

所以基本上根据我的理解,我需要一个活动和一个用于该活动的布局,其中定义了一个列表视图。我需要创建我的自定义适配器。另外,我需要创建一个列表布局,其中包含信息和一个视图(带有 3 个点),作为启动弹出菜单的按钮?我在这里看到的问题是我如何仅为该视图创建一个侦听器以及如何在列表视图中引用该特定列表项的值。

我还没有任何可用的代码,因为我还没有开始任何与此相关的事情。我目前正在获取理论上的信息,但如果需要,我将创建一个示例代码。

谢谢。

4

7 回答 7

22

使用弹出菜单,通过以下三个步骤创建菜单非常简单:

1 -OnClickListener使用或我喜欢的布局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 布局并显示它:

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();
}
于 2016-01-03T02:01:30.693 回答
8

你可以这样使用:

public class MainActivity extends Activity {
    ListView listView_Actions;
    ArrayList<String> actionsArrayList;
    Button btn_ViewPopUp;
    ArrayAdapter<String> actionsAdapter;
    static final int CUSTOM_DIALOG_ID1 = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_ViewPopUp=(Button) findViewById(R.id.btn_ViewPopUp);

        actionsArrayList=new ArrayList<String>();
        actionsArrayList.add("Action 1");
        actionsArrayList.add("Action 2");
    }

    @Override
    protected void onStart() {
        super.onStart();

        btn_ViewPopUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(CUSTOM_DIALOG_ID1);
                actionsAdapter = new MyCustomBaseAdapter(getApplicationContext(), R.layout.list_actions, actionsArrayList);
                listView_Actions.setAdapter(actionsAdapter);
            }
        });
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        Dialog dialog = null;
        switch (id) {
            case CUSTOM_DIALOG_ID1:
                dialog = new Dialog(MainActivity.this);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                dialog.setContentView(R.layout.list_actions);
                listView_Actions = (ListView) dialog.findViewById(R.id.listView_Actions);
                break;
        }
        return dialog;
    }

    class MyCustomBaseAdapter extends ArrayAdapter<String> 
    {
        public MyCustomBaseAdapter(Context context, int textViewResourceId, ArrayList<String> actionsArrayList) {
            super(context, textViewResourceId,actionsArrayList);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = inflater.inflate(R.layout.action_list_cell, null);
            final TextView lblContactAction;
            lblContactAction = (TextView) v.findViewById(R.id.txtContactAction);

            lblContactAction.append(actionsArrayList.get(position));
            return v;
        }
    }
}

现在 XML 文件:

action_list_cell.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@android:color/background_light" >

    <TextView
        android:id="@+id/txtContactAction"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
         android:textSize="18dp"
        android:textColor="@android:color/black" />

</LinearLayout>

list_actions.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@drawable/rounded_corner_top">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#DB6A16"
            android:orientation="vertical"
            android:paddingBottom="2dp"
            android:paddingLeft="2dp"
            android:paddingRight="2dp" >

            <ListView
                android:id="@+id/listView_Actions"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#ffffff" >
            </ListView>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>
于 2013-09-03T10:06:47.603 回答
8

ActionBarCompat List PopupMenu 实现在这里(有可用的后端端口,因为它使用 ABC)!

在此处输入图像描述

您也可以从Github或 SDK中获取此示例(Mr.Morgan 评论如下)

/sdk/samples/android-19/ui/ActionBarCompat-ListPopupMenu。确保在 Android 4.4.2 (API 19) 下安装 SDK 示例

于 2014-02-07T11:49:29.360 回答
4

first of all you need to make your custom adapter with a view that has the 3 dots.

then in the getView() or newView() method you set the listener to the 3 dots image.

i think that PopupMenu is what you are looking for, it's is supported since API 11.

if you want to support also earlier version of the API you can use PopupMenu class provided by the support library v7.

the usage is pretty straight forward. you define it with the id of the view you want the menu to show next to, and then you can directly inflate a menu resource there as if it was a common menu.

于 2013-09-03T09:44:24.703 回答
2

现在 showDialog 已弃用,请改用 PopupMenu 和 AppCompat PopupMenu 如果您想支持 V11 之前的版本

public class MainActivity extends Activity {  
Button button1;  

         @Override  
         protected void onCreate(Bundle savedInstanceState) {  
          super.onCreate(savedInstanceState);  
          setContentView(R.layout.activity_main);  

          button1 = (Button) findViewById(R.id.button1);  
          button1.setOnClickListener(new OnClickListener() {  

           @Override  
           public void onClick(View v) {  
            //Creating the instance of PopupMenu  
            PopupMenu popup = new PopupMenu(MainActivity.this, button1);  
            //Inflating the Popup using xml file  
            popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());  

            //registering popup with OnMenuItemClickListener  
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {  
             public boolean onMenuItemClick(MenuItem item) {  
              Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();  
              return true;  
             }  
            });  

            popup.show();//showing popup menu  
           }  
          });//closing the setOnClickListener method  
         }  
    }  
于 2015-02-09T13:42:33.650 回答
0

不确定我是否理解正确,但您可以触发此方法以打开带有列表视图的弹出对话框。

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Title if Any");
    builder.setItems(R.array.listoptions, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int itemClicked) {
                   String[] option_array = getResources().getStringArray(R.array.listoptions);

                   String optionSelected = option_array[itemClicked];
           }
    });
    return builder.create();
}

请参阅添加列表

 <?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="listoption">
    <item>Install</item>
    <item>Add to listview</item>
</string-array>
</resources>

希望这可以帮助。

于 2013-09-03T09:50:20.723 回答
0

您必须在 List-Adapter 的 getView() 方法中设置按钮的侦听器。在此 getView() 方法中,您将布局分配给一个列表项。如果你已经这样做了,你只需要在这个视图(按钮)上设置监听器,并处理 onClick() 事件。

于 2013-09-03T09:42:02.253 回答