-
一\重寫onCreateOptionsMenu()方法: @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); //設置子菜單 SubMenu file = menu.addSubMenu("文件"); SubMenu edit = menu.addSubMenu("编辑"); //為“文件”選項添加子菜單,groupID為1 file.add(1, 1, 1, "新建"); file.add(1, 2, 1, "打开"); file.add(1, 3, 1, "保存"); file.setHeaderTitle("文件操作");//子菜单标题 file.setHeaderIcon(R.drawable.ic_launcher);//子菜单图标 //為“編輯”選項添加子菜單,groupID為2 edit.add(2, 1, 1, "复制"); edit.add(2, 2, 1, "粘贴"); edit.add(2, 3, 1, "剪切"); edit.setHeaderTitle("编辑操作"); edit.setHeaderIcon(R.drawable.ic_launcher); return true; }查看全部
-
2)重寫 onCreateContextMenu()方法 @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { // TODO Auto-generated method stub super.onCreateContextMenu(menu, v, menuInfo); //设置Menu显示内容 menu.setHeaderTitle("文件操作"); menu.setHeaderIcon(R.drawable.ic_launcher); //將XML文件引入 MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); }查看全部
-
六、出來java代碼動態添加外還可以通過xml實現上下文菜單 1)res->menu->main.xml: <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/context_menu_item1" android:orderInCategory="100" android:showAsAction="never" android:title="复制"/> <item android:id="@+id/context_menu_item2" android:orderInCategory="100" android:showAsAction="never" android:title="粘贴"/> <item android:id="@+id/context_menu_item3" android:orderInCategory="100" android:showAsAction="never" android:title="剪切"/> <item android:id="@+id/context_menu_item4" android:orderInCategory="100" android:showAsAction="never" android:title="重命名"/> </menu>查看全部
-
五、重寫點擊事件: @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.context_menu_item1: Toast.makeText(MainActivity.this, "点击复制",Toast.LENGTH_SHORT).show(); break; case R.id.context_menu_item2: Toast.makeText(MainActivity.this, "点击粘贴",Toast.LENGTH_SHORT).show(); break; case R.id.context_menu_item3: Toast.makeText(MainActivity.this, "点击剪切",Toast.LENGTH_SHORT).show(); break; case R.id.context_menu_item4: Toast.makeText(MainActivity.this, "点击重命名",Toast.LENGTH_SHORT).show(); break; } return super.onContextItemSelected(item); }查看全部
-
四.重寫onCreateContextMenu方法: @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { // TODO Auto-generated method stub super.onCreateContextMenu(menu, v, menuInfo); //设置Menu显示内容 menu.setHeaderTitle("文件操作"); menu.setHeaderIcon(R.drawable.ic_launcher); //添加菜單項 menu.add(1, 1, 1, "复制"); menu.add(1, 2, 1, "粘贴"); menu.add(1, 3, 1, "剪切"); menu.add(1, 4, 1, "重命名"); }查看全部
-
一、創建一個xml文件listview <ListView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/listview" /> 二./** * 构造listview显示的数据 * * @return */ private ArrayList<String> getData() { ArrayList<String> list = new ArrayList<String>(); for (int i = 0; i < 5; i++) { list.add("文件" + (i + 1)); } return list; } 三./** * 设置listview的显示内容 */ private void showListView() { ListView listview = (ListView) findViewById(R.id.listview); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getData()); //為listview設置適配器 listview.setAdapter(adapter); //為listview設置上下文菜單 this.registerForContextMenu(listview); }查看全部
-
上下文菜单的创建查看全部
-
上下文菜单和选项菜单的区别查看全部
-
ContextMenu的组成查看全部
-
根据手势不同弹出的菜单为上下文菜单查看全部
-
點擊跳轉到secondActivity: Intent intent = new Intent(MainActivity.this, SecondActivity.class); item.setIntent(intent);查看全部
-
二.點擊事件 @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case 100: Intent intent = new Intent(MainActivity.this, SecondActivity.class); item.setIntent(intent); break; case 101: Toast.makeText(MainActivity.this, "点击了菜单二", Toast.LENGTH_SHORT) .show(); break; case 102: Toast.makeText(MainActivity.this, "点击了菜单三", Toast.LENGTH_SHORT) .show(); break; case 103: Toast.makeText(MainActivity.this, "点击了菜单四", Toast.LENGTH_SHORT) .show(); break; case 104: Toast.makeText(MainActivity.this, "点击了菜单五", Toast.LENGTH_SHORT) .show(); break; case 105: Toast.makeText(MainActivity.this, "点击了菜单六", Toast.LENGTH_SHORT) .show(); break; case 106: Toast.makeText(MainActivity.this, "点击了菜单七", Toast.LENGTH_SHORT) .show(); break; } return super.onOptionsItemSelected(item); }查看全部
-
一、javad代碼動態添加菜單 public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. // getMenuInflater().inflate(R.menu.main, menu); MenuItem item = menu.add(1/*分組的ID(GroupID)*/, 100,/*菜單項的ID*/ 1, /*菜單項的排序*/ "菜单一"); item.setTitle("aaa"); item.setIcon(R.drawable.ic_launcher);// api>=11 不显示图标 , menu.add(1, 101, 1, "菜单二"); menu.add(1, 102, 1, "菜单三"); menu.add(1, 103, 1, "菜单四"); menu.add(1, 104, 1, "菜单五"); menu.add(1, 105, 1, "菜单六"); menu.add(1, 106, 1, "菜单七"); return true; }查看全部
-
// Inflate the menu; this adds items to the action bar if it is present. // getMenuInflater().inflate(R.menu.main, menu); 二、res->menu->main.xml: <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_menu_item1" android:orderInCategory="100" android:showAsAction="never" android:title="菜单一"/> <item android:id="@+id/action_menu_item2" android:orderInCategory="100" android:showAsAction="never" android:title="菜单二"/> </menu>查看全部
-
两种create OptionsMenu的方法查看全部
举报
0/150
提交
取消