不多说,先上码。
//activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="菜单界面"
android:textSize="20dp"
/>
</LinearLayout>
//res文件下menu中的main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/item1"
android:orderInCategory="100"
android:showAsAction="never"
android:title="菜单一"/>
<item
android:id="@+id/item2"
android:orderInCategory="100"
android:showAsAction="never"
android:title="菜单二"/>
</menu>
这里是通过main.xml设置菜单。
//MainActivity.java
package com.example.test_optionmenu;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//加载main.xml中的菜单item
getMenuInflater().inflate(R.menu.main, menu);
//动态加载菜单项
menu.add(1, 10, 101, "菜单三"); //(groupId,itemId,order,title)-->(分组id,菜单id,菜单项排序,菜单名)
menu.add(1, 11, 102, "菜单四");
return true;
}
//监听菜单选择操作
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.item1:
//点击菜单一之后会跳转到相对应的界面(实现意图)
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
Toast.makeText(this,"点击了菜单一",1000).show();
break;
case R.id.item2:
Toast.makeText(this,"点击了菜单二",1000).show();
break;
case 10:
Toast.makeText(this,"点击了菜单三",1000).show();
break;
case 11:
Toast.makeText(this,"点击了菜单四",1000).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
这里需要说明,创建菜单有两种方式:①通过main.xml文件去创建,然后通过getMenuInflater().inflate(R.menu.main, menu);
去加载;②通过menu.add()动态加载菜单,其中menu.add(groupId,itemId,order,title)中的四个参数分别代表:(分组id,菜单id,菜单项排序,菜单名)。一般来说在同一个按键下的菜单groupId我们是设置同一个分组,这里可以都设成1;itemId是识别菜单的唯一标识,因此每个id都不一样,要为整数;order是排列的顺序,从小到大往下排,其中通过main.xml创建的菜单order是100
(> android:orderInCategory=”100”)
,小于100的则在上,大于100的在下;menu是菜单名字。
//我们需要为菜单一的点击创建新的activity和xml
//second_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这是菜单一点击启动的界面"
/>
</LinearLayout>
//SecondActivity.java
package com.example.test_optionmenu;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
}
}
千万别忘记在AndroidManifest中添加SecondActivity的权限哦。
<activity
android:name="com.example.test_optionmenu.SecondActivity"
android:label="@string/app_name" >
</activity>