子菜单,含有菜单项的菜单
添加方式:静态xml/动态添加
menu/main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:showAsAction="never"
android:title="文件">
<menu>
<item
android:id="@+id/new_file"
android:showAsAction="never"
android:title="新建"/>
<item
android:id="@+id/open_file"
android:showAsAction="never"
android:title="打开"/>
<item
android:id="@+id/save_file"
android:showAsAction="never"
android:title="保存"/>
</menu>
</item>
<item
android:showAsAction="never"
android:title="编辑">
<menu>
<item
android:id="@+id/c_edit"
android:showAsAction="never"
android:title="复制"/>
<item
android:id="@+id/v_edit"
android:showAsAction="never"
android:title="粘贴"/>
<item
android:id="@+id/x_edit"
android:showAsAction="never"
android:title="剪切"/>
</menu>
</item>
</menu>
MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
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);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
// SubMenu file = menu.addSubMenu("文件");
// SubMenu edit = menu.addSubMenu("编辑");
// file.add(1, 1, 1, "新建");
// file.add(1, 2, 1, "打开");
// file.add(1, 3, 1, "保存");
// file.setHeaderTitle("文件操作");//子菜单标题
// file.setHeaderIcon(R.drawable.ic_launcher);//子菜单图标
// edit.add(2, 1, 1, "复制");
// edit.add(2, 2, 1, "粘贴");
// edit.add(2, 3, 1, "剪切");
// edit.setHeaderTitle("编辑操作");
// edit.setHeaderIcon(R.drawable.ic_launcher);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.new_file:
Toast.makeText(this, "点击了新建",Toast.LENGTH_SHORT).show();
break;
case R.id.open_file:
Toast.makeText(this, "点击了打开",Toast.LENGTH_SHORT).show();
break;
case R.id.save_file:
Toast.makeText(this, "点击了保存",Toast.LENGTH_SHORT).show();
break;
case R.id.c_edit:
Toast.makeText(this, "点击了复制",Toast.LENGTH_SHORT).show();
break;
case R.id.v_edit:
Toast.makeText(this, "点击了粘贴",Toast.LENGTH_SHORT).show();
break;
case R.id.x_edit:
Toast.makeText(this, "点击了剪切",Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
}
效果演示:
sub.gif
网友评论