Android layout 列表视图的上下文菜单

Android layout 列表视图的上下文菜单,android-layout,listview,contextmenu,Android Layout,Listview,Contextmenu,我使用的是列表视图。我使用适配器添加此列表的项目。我正在尝试为列表中的每个项目添加contextmenu,但我在这方面遇到了问题 这是我的密码 项目清单: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@

我使用的是列表视图。我使用适配器添加此列表的项目。我正在尝试为列表中的每个项目添加contextmenu,但我在这方面遇到了问题

这是我的密码

项目清单:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bgActivity"
android:orientation="horizontal" >

<TextView
    android:id="@+id/tv_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:textColor="@color/textColorContact"
    android:textSize="20sp"
    android:textStyle="bold" />

<Button
    android:id="@+id/pick_options"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginRight="10dp"
    android:background="@drawable/app_icon"
    android:text="@string/label_options" >
</Button>   

</RelativeLayout>

但是我没有看到上下文菜单,请帮助,谢谢

我想你应该尝试添加(在布局中)

在您的布局中。 你可以看看 我建议您将上下文菜单与ActionMode.Callback一起使用。如果你想要一个例子,看看

注意:链接来自我的博客

<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" >

<ListView
    android:id="@+id/lst_contacts"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/bgContactItem" />

</RelativeLayout>
<menu 
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/CtxLblOpc1" android:title="OpcEtiqueta1"></item>
<item android:id="@+id/CtxLblOpc2" android:title="OpcEtiqueta2"></item>
</menu>
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contacts);

// Getting reference to listview
    ListView lstContacts = (ListView) findViewById(R.id.lst_contacts);
registerForContextMenu(lstContacts);
}

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


}


/** Actions for the ContextMenu items */
@Override
public boolean onContextItemSelected(MenuItem item) {
  //AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
  switch (item.getItemId()) {
      case R.id.CtxLblOpc1:
        Toast.makeText(getApplicationContext(), 
                         "Option 1",     
                         Toast.LENGTH_LONG).show();
          return true;
      case R.id.CtxLblOpc2:
        Toast.makeText(getApplicationContext(), 
                         "Option 2", 
                         Toast.LENGTH_LONG).show();
          return true;
      default:
          return super.onContextItemSelected(item);
   }
}
android:descendantFocusability="blocksDescendants"