Android中的Menu,AndroidMenu

來源:互聯網
上載者:User

Android中的Menu,AndroidMenu

Android中的設定按鈕:長按或點擊菜單鍵

1.長按選項:

布局檔案:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="vertical" 6     android:paddingBottom="@dimen/activity_vertical_margin" 7     android:paddingLeft="@dimen/activity_horizontal_margin" 8     android:paddingRight="@dimen/activity_horizontal_margin" 9     android:paddingTop="@dimen/activity_vertical_margin"10     tools:context=".MenuActivity" >11 12     <TextView13         android:id="@+id/menutext1"14         android:layout_width="fill_parent"15         android:layout_height="wrap_content"16         android:text="第一種方法建立菜單"17         android:textSize="20sp" />18 19     <TextView20         android:id="@+id/menutext2"21         android:layout_width="fill_parent"22         android:layout_height="wrap_content"23         android:text="第二種方法建立菜單"24         android:textSize="20sp" />25 26     <ListView27         android:id="@+id/menulist"28         android:layout_width="fill_parent"29         android:layout_height="fill_parent"30         android:layout_marginTop="50dp" >31     </ListView>32 33 </LinearLayout>

實現過程:

 1 private ListView list; 2     private TextView lv; 3  4     String[] str = { "TextView", "EditView", "Toast(訊息框)" }; 5  6     @Override 7     protected void onCreate(Bundle savedInstanceState) { 8         super.onCreate(savedInstanceState); 9         setContentView(R.layout.activity_menu);10 11         list = (ListView) findViewById(R.id.menulist);12         ArrayAdapter<String> arr = new ArrayAdapter<String>(this,13                 android.R.layout.simple_list_item_1, str);14         list.setAdapter(arr);15 16         list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {17 18             @Override19             public void onCreateContextMenu(ContextMenu menu, View v,20                     ContextMenuInfo menuInfo) {21                 menu.clear();22                 menu.clearHeader();23                 menu.setHeaderIcon((R.drawable.ic_launcher));24                 menu.setHeaderTitle("我是ListView");25                 menu.add(3, 6, 0, "刪除");26                 menu.add(3, 7, 1, "修改");27             }28         });29         // 第一種方法30         TextView tv = (TextView) findViewById(R.id.menutext1);31         this.registerForContextMenu(tv);32         // 第二種方法33         lv = (TextView) findViewById(R.id.menutext2);34         lv.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {35 36             @Override37             public void onCreateContextMenu(ContextMenu menu, View v,38                     ContextMenuInfo menuInfo) {39                 menu.setHeaderTitle("長度-ContextMenu");40                 menu.add(0, 1, 0, "複製");41                 SubMenu sb = menu.addSubMenu("尋找");42                 sb.add(1, 2, 0, "按id尋找");43                 sb.add(1, 3, 0, "按名稱尋找");44             }45         });46     }

2.長按功能表項目:

 1 private ListView list; 2     private TextView lv; 3  4     String[] str = { "TextView", "EditView", "Toast(訊息框)" }; 5  6     @Override 7     protected void onCreate(Bundle savedInstanceState) { 8         super.onCreate(savedInstanceState); 9         setContentView(R.layout.activity_menu);10 11         list = (ListView) findViewById(R.id.menulist);12         ArrayAdapter<String> arr = new ArrayAdapter<String>(this,13                 android.R.layout.simple_list_item_1, str);14         list.setAdapter(arr);15 16         list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {17 18             @Override19             public void onCreateContextMenu(ContextMenu menu, View v,20                     ContextMenuInfo menuInfo) {21                 menu.clear();22                 menu.clearHeader();23                 menu.setHeaderIcon((R.drawable.ic_launcher));24                 menu.setHeaderTitle("我是ListView");25                 menu.add(3, 6, 0, "刪除");26                 menu.add(3, 7, 1, "修改");27             }28         });29         // 第一種方法30         TextView tv = (TextView) findViewById(R.id.menutext1);31         this.registerForContextMenu(tv);32         // 第二種方法33         lv = (TextView) findViewById(R.id.menutext2);34         lv.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {35 36             @Override37             public void onCreateContextMenu(ContextMenu menu, View v,38                     ContextMenuInfo menuInfo) {39                 menu.setHeaderTitle("長度-ContextMenu");40                 menu.add(0, 1, 0, "複製");41                 SubMenu sb = menu.addSubMenu("尋找");42                 sb.add(1, 2, 0, "按id尋找");43                 sb.add(1, 3, 0, "按名稱尋找");44             }45         });46     }

Over

   標籤: Android, menu

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.