标签:
package com.example.menu2; import android.app.Activity; import android.content.Intent; import android.os.Bundle; 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) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.create: Toast.makeText(MainActivity.this, "创建文件", Toast.LENGTH_SHORT).show(); break; case R.id.open: Intent intent = new Intent(MainActivity.this, NextActivity.class); item.setIntent(intent); // Toast.makeText(MainActivity.this, "打开文件", Toast.LENGTH_SHORT).show(); break; case R.id.load: Toast.makeText(MainActivity.this, "加载", Toast.LENGTH_SHORT).show(); break; case R.id.save: Toast.makeText(MainActivity.this, "保存", Toast.LENGTH_SHORT).show(); break; default: break; } return super.onOptionsItemSelected(item); } }
主布局文件可以不用管
<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" > </RelativeLayout>res/menu/main.xml定义菜单
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <!-- orderInCategory菜单顺序类别 --> <!-- app:showAsAction="never"表示不会在标题栏上面显示 --> <item android:id="@+id/menu_settings" android:icon="@drawable/communication" android:orderInCategory="100" android:showAsAction="ifRoom|withText" android:title="菜单设置"> <menu> <item android:id="@+id/create" android:title="新建文件"> </item> <item android:id="@+id/open" android:title="打开文件"> </item> </menu> </item> <item android:id="@+id/system" android:icon="@drawable/gear" android:orderInCategory="101" android:showAsAction="ifRoom|withText" android:title="用户菜单"> <menu> <group android:id="@+id/group1" > <item android:id="@+id/load" android:title="加载"> </item> </group> <group android:id="@+id/group2" > <item android:id="@+id/save" android:title="保存"> </item> </group> </menu> </item> </menu>定义一个菜单跳转的页面
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="172dp" android:textSize="30sp" android:text="这是跳转的页面" /> </RelativeLayout>
package com.example.menu2; import android.app.Activity; import android.os.Bundle; public class NextActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_next); } }
showAsAction指定菜单显示的位置这里是never
如果改为ifRoom|withText,表示如果actionBar有空间就显示在actionBar而且显示标题
这个工程在模拟器上调试正常,在真机上调试二级菜单打不开,不知道是不是手机问题
标签:
原文地址:http://blog.csdn.net/lindonglian/article/details/43908147