Android为Menu添加item及响应函数
2014-08-25 17:15
176 查看
参考 http://www.crifan.com/android_add_menu/
响应menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.e(TAG,"onCreateOptionMenu");
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
menu.add("插入");
return true;
}
响应menu的条目
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.e("ZHANGBIN","create option menu");
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_discard:
Toast.makeText(MainActivity.this, "Menu Discard cliked", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_send:
Toast.makeText(MainActivity.this, "Menu Send cliked", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
menu的布局xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_discard"
android:orderInCategory="1"
android:showAsAction="ifRoom|withText"
android:title="试试"/>
<!-- android:icon="@drawable/error_white"
android:title="@string/discard"
android:icon="@drawable/forward_white"-->
<item
android:id="@+id/menu_send"
android:orderInCategory="2"
android:showAsAction="ifRoom|withText"
android:title="看看"/>
</menu>
没深入学习,可以参考
http://tech.it168.com/a2011/1031/1266/000001266576_all.shtml
学习下每个item的xml给定的属性的意义。
响应menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.e(TAG,"onCreateOptionMenu");
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
menu.add("插入");
return true;
}
响应menu的条目
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.e("ZHANGBIN","create option menu");
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_discard:
Toast.makeText(MainActivity.this, "Menu Discard cliked", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_send:
Toast.makeText(MainActivity.this, "Menu Send cliked", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
menu的布局xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_discard"
android:orderInCategory="1"
android:showAsAction="ifRoom|withText"
android:title="试试"/>
<!-- android:icon="@drawable/error_white"
android:title="@string/discard"
android:icon="@drawable/forward_white"-->
<item
android:id="@+id/menu_send"
android:orderInCategory="2"
android:showAsAction="ifRoom|withText"
android:title="看看"/>
</menu>
没深入学习,可以参考
http://tech.it168.com/a2011/1031/1266/000001266576_all.shtml
学习下每个item的xml给定的属性的意义。
相关文章推荐
- eclipse 编写Android程序时,在menu添加菜单节点没有“Item”之类的选项
- 为Android内核添加新驱动,并添加到menuconfig菜单
- 【转】Android statusBar添加back,home,menu按钮
- android中ListView在划屏到底部的时候动态添加ListView的Item实现
- 在Android 2.3状态栏中添加menu,home和back快捷键的方法
- Android中ListView的item中添加图片和文字
- Extjs动态添加子菜单:Ext.menu.Item 动态添加 Ext.menu.Menu
- Android 2.3状态栏中添加menu,home和back快捷键
- (转)为Android内核添加新驱动,并添加到menuconfig菜单【实践简记】
- android menu.addIntentOptions 添加动态菜单
- android 为Tab添加Menu
- [转]Android statusBar添加back,home,menu按钮
- [转]Android statusBar添加back,home,menu按钮
- 对在Android的状态栏添加Menu,Back,Home的改进
- 在Android 2.3状态栏中添加menu,home和back快捷键的方法
- Android里添加 Menu
- Android statusBar添加back,home,menu按钮
- 在Android 2.3状态栏中添加menu,home和back快捷键的方法
- Android中添加Options Menu,按MENU键无反应
- android 为 ListView Item中的组件添加事件 以及更新数据