代码之家  ›  专栏  ›  技术社区  ›  Georgy Gobozov

列表活动中的上下文菜单

  •  3
  • Georgy Gobozov  · 技术社区  · 14 年前

    我有自定义数组适配器的列表活动,长按列表项时无法获取上下文菜单。

    <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"/>
        <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="5dp">
            <ListView
                    android:id="@+id/list"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    />
    
        </FrameLayout>
    

    列表项布局

        <?xml version="1.0" encoding="utf-8"?>
         <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
            >
        <ImageView
                android:id="@+id/icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
        <TextView
                android:id="@+id/info"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:textSize="15sp"
                android:singleLine="true"
                android:ellipsize="marquee"
                android:scrollHorizontally = "true"
                android:maxWidth="200dp"
                />
    
    
        <LinearLayout
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                 android:gravity="right"
                >
            <ImageButton
                    android:id="@+id/button"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:background="@null"
                    android:paddingRight="10dp"                
                    android:paddingLeft="10dp"
    
    
                    />
        </LinearLayout>
    
    </LinearLayout>
    

    在活动中

    @Override
    public void onCreate(Bundle savedInstanceState) {
       registerForContextMenu(getListView());
    }
    
        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.context_menu, menu);
    
    
    
      }
    

    为什么我看不到上下文菜单?我做错了什么?如何使用数组适配器和ListActivity获取上下文菜单。 谢谢!

    2 回复  |  直到 14 年前
        2
  •  1
  •   David Salgado    13 年前

    您需要为列表项布局文件中的第一个LinearLayout设置属性LongClickable:

        <?xml version="1.0" encoding="utf-8"?>
         <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:longClickable="true"
            >
    ...