Android的定制下拉/弹出菜单 [英] Android custom dropdown/popup menu

查看:192
本文介绍了Android的定制下拉/弹出菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该怎么做锚定到一个按钮自定义下拉/弹出菜单?

How do I do a custom dropdown/popup menu anchored to a button?

我需要它的工作,如弹出菜单(锚定到一个视图),并做当我点击菜单中的项目什么的。

I need it to work like the popup menu (anchored to a view), and do something when I click an item from the menu.

我如何将项目添加到菜单,code,保持菜单的高度,使其滚动的,如果有超过5个项目。我不需要添加任何图像,只是文本。

How do I add items to the menu by code, keeping menu's height and make it scrollable if there are more than 5 items. I don't need to add any images, just text.

推荐答案

要创建弹出菜单中的机器人。

To create popup menu in android.

activity_main.xml

activity_main.xml

它只有一个按钮。

文件:activity_main.xml

File: activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:paddingBottom="@dimen/activity_vertical_margin"  
    android:paddingLeft="@dimen/activity_horizontal_margin"  
    android:paddingRight="@dimen/activity_horizontal_margin"  
    android:paddingTop="@dimen/activity_vertical_margin"  
    tools:context=".MainActivity" >  

    <Button  
        android:id="@+id/button1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentLeft="true"  
        android:layout_alignParentTop="true"  
        android:layout_marginLeft="62dp"  
        android:layout_marginTop="50dp"  
        android:text="Show Popup" />  

</RelativeLayout>  

popup_menu.xml

popup_menu.xml

它包含三个项目如下显示。它是在RES /菜单目录中创建的。 文件:poupup_menu.xml

It contains three items as show below. It is created inside the res/menu directory. File: poupup_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >  

    <item  
        android:id="@+id/one"  
        android:title="One"/>  

    <item  
        android:id="@+id/two"  
        android:title="Two"/>  

    <item  
        android:id="@+id/three"  
        android:title="Three"/>  

</menu>  

Activity类

Activity class

它显示按钮在弹出的菜单中。 文件:MainActivity.java

It displays the popup menu on button click. File: MainActivity.java

public class MainActivity extends Activity {  
    private 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
    }
}

请<一href="http://stackoverflow.com/questions/13784088/setting-popupmenu-menu-items-programmatically">this链接创建菜单编程。 <一href="http://stackoverflow.com/questions/13784088/setting-popupmenu-menu-items-programmatically">This链接,自定义它。

Follow this link for creating menu programmatically. This link for customizing it.

这篇关于Android的定制下拉/弹出菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆