Android Tutorial(3)Android Menu Example - Utilizing Menus

本文介绍如何在Android应用中创建和使用菜单。通过创建XML文件定义菜单项,并在Activity中实现菜单响应逻辑。解决常见错误,如资源标识符未找到等问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android Tutorial(3)Android Menu Example - Utilizing Menus

Trouble Shooting First
Error Message:
[INFO] :13: error: No resource identifier found for attribute 'textIsSelectable' in package 'android'
[ERROR] Error when generating sources.
org.apache.maven.plugin.MojoExecutionException:
at com.jayway.maven.plugins.android.phase01generatesources.GenerateSourcesMojo.generateR(GenerateSourcesMojo.java:446)
at com.jayway.maven.plugins.android.phase01generatesources.GenerateSourcesMojo.execute(GenerateSourcesMojo.java:162)

Solution:
That is because of the API Level. So try to get rid of this from XML configurations.

Error Message:
GcmBaseIntentService registration error account_missing

Solution:
Directly use Setting to login on with a google account.

I plan to note how to set the menus.

1. Create the XML file
Create the menu file from [res]------>[menu]----->[New Android XML File] ----->
[Resource Type] ----> Menu
Root Element -----> menu

Just click [Add Item] to add menus.

I set these properties ---> id, Title, Icon[@drawable/menu_name] menu_name is the name of the icon file in drawable-hdpi.

2. Modify the Implementation Activity Class
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.options_menu_all, menu);
returntrue;
}


publicboolean onOptionsItemSelected(MenuItem item) {
boolean result = true;
try {
switch (item.getItemId()) {
caseR.id.item_list_all_person:
startActivity(new Intent(this, PersonListActivity.class));
return true;
caseR.id.item_get_one_person:
startActivity(new Intent(this, GetOnePersonActivity.class));
return true;
caseR.id.item_gcm_demo:
startActivity(new Intent(this, DemoActivity.class));
return true;
caseR.id.item_list_product:
startActivity(new Intent(this, ProductsListActivity.class));
return true;
default:
returnsuper.onOptionsItemSelected(item);
}
} catch (Exception error) {
Log.d(TAG, "About_onOptionsItemSelected failed");
}
return result;
}

Some Tips:
1. If you want to show the selected item in menu. We can ask the different activity load different menu XML files with different icons.

2. We can also put the implementation of the menu in abstract class of the activities which extended from base class. That is to say, just put the menu in the base class is fine.


References:
http://vimaltuts.com/android-tutorial-for-beginners/android-menu-example
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值