在Android中打开浮动菜单(上下文菜单)? [英] Opening a floating menu (context menu) in Android?

查看:85
本文介绍了在Android中打开浮动菜单(上下文菜单)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新菜单,名为drmenu.xml。当我按下菜单按钮时,它可以正常工作,但是当用户按下按钮时,我需要打开上下文菜单。
下面的代码按钮仅显示一个敬酒。

I created a new menu, named drmenu.xml. It works correctly when i press menu button, but I need to open a context menu when the user press the button. The below code the button just show a toast.

这是我的xml布局:

 <LinearLayout
        android:id="@+id/MenuCall"
        android:layout_width="90dip"
        android:layout_height="match_parent"
        android:gravity="right|center_vertical" >
        <ImageView
            android:id="@+id/MenuCall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/imageiew6" />
    </LinearLayout>

这是我的Java代码:

and this is my java code:

    final LinearLayout callback_var = (LinearLayout) findViewById(R.id.MenuCall);
    registerForContextMenu(callback_var);
    callback_var.setOnClickListener(new android.view.View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "this is repeated",      Toast.LENGTH_LONG).show();
            openContextMenu(callback_var);
        }


推荐答案

如果要创建上下文菜单,则必须调用方法 registerForContextMenu()向其传递应该与上下文菜单关联的View。

If you want create a context Menu, you have to call the method registerForContextMenu() passing it the View that should be associated with the context menu.

例如,假设要关联上下文菜单

For example, supposing to associate the context menu with a Button:

Button button = (Button) findViewById(R.id.my_button);
registerForContextMenu(button);

可以在您的Activity的onCreate()中调用。 ,您需要覆盖 onCreateContextMenu() m

which can be called in the onCreate() of your Activity. Then, inside the same activity, you need to override the onCreateContextMenu() method.

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.your_context_menu, menu);
}

然后您必须实现 onContextItemSelected(),用于在按下上下文菜单中的项目时触发适当的操作:

Then you have to implement onContextItemSelected(), for triggering the proper action when a item inside the context menu is pressed:

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.first_action:
            // your first action code
            return true;
        case R.id.second_action:
            // your second action code
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}

现在,长按按钮可打开上下文您在 your_context_menu.xml 文件中定义的菜单。

Now the long press click on the button opens the context menu you defined in the your_context_menu.xml file.

考虑到长按打开上下文菜单是符合要求的使用Android标准用户界面,但是,如果您希望在快捷菜单上显示上下文菜单,则可以在此处找到答案

Consider that opening the context menu with a long press is compliant with the Android standard UI, however If you want your context menu to appear on a simple tap you can look here the answer

注意:
如所述此处


整棵树,但是在您要搜索的树的一部分中
应该是唯一的(通常
可能是整棵树,因此最好在
可能的情况下完全唯一)。 / p>

An ID need not be unique throughout the entire tree, but it should be unique within the part of the tree you are searching (which may often be the entire tree, so it's best to be completely unique when possible).

这篇关于在Android中打开浮动菜单(上下文菜单)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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