目标效果:
程序运行,点击右上角的三个点可现实菜单列表,点击列表中的“新页面”,可以跳转到图二所示的第二个页面,点击列表中“设置”和“确定”,可以吐司打印信息。
1.activity_main.xml页面放置TextView控件。
activity_main.xml页面:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tvOne" /> </RelativeLayout>
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 添加选项菜单 android:orderInCategory为排序序号 showAsAction为默认是否显示(never)默认不显示 android:title为菜单标题 --> <item android:id="@+id/action_set" android:orderInCategory="100" android:showAsAction="never" android:title="@string/action_set"/> <item android:id="@+id/action_sure" android:orderInCategory="100" android:showAsAction="never" android:title="@string/action_sure"/> </menu>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">选项菜单</string> <string name="action_settings">Settings</string> <string name="tvOne">第一个页面</string> <string name="action_sure">确定</string> <string name="action_set">设置</string> <string name="title_activity_second">SecondActivity</string> <string name="tvTwo">第二个页面</string> </resources>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".SecondActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tvTwo" /> </RelativeLayout>
package com.example.optionmenu; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.MenuItem; 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) { getMenuInflater().inflate(R.menu.main, menu); /*动态添加菜单*/ //参数一位分组的id,参数二为菜单项id,参数三为菜单项排序,参数四为菜单描述 menu.add(1,100,1,"新页面"); return true; } /*菜单点击事件*/ @Override public boolean onOptionsItemSelected(MenuItem item) {//item为当前点击的菜单 switch (item.getItemId()) { case R.id.action_set: Toast.makeText(MainActivity.this,"点击了设置菜单",Toast.LENGTH_SHORT).show(); break; case R.id.action_sure: Toast.makeText(MainActivity.this,"点击了确定菜单",Toast.LENGTH_SHORT).show(); break; case 100: Intent intent=new Intent(MainActivity.this,SecondActivity.class);//创建意图 item.setIntent(intent);//跳转 break; } return super.onOptionsItemSelected(item); } }