码迷,mamicode.com
首页 > 移动开发 > 详细

Android学习笔记之Menu一级菜单、二级菜单的使用

时间:2015-01-17 13:57:26      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:android学习笔记之menu一级菜单   menu一级菜单二级菜单的使用   

(1)布局文件没有做更改

(2)在res--menu目录下的main.xml文件中代码如下:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 一级菜单 -->
    <item
        android:id="@+id/file"
        android:title="@string/file">

        <!-- 二级菜单 -->
        <menu>
            <item
                android:id="@+id/create"
                android:title="@string/create">
            </item>
            <item
                android:id="@+id/open"
                android:title="@string/open">
            </item>
        </menu>
    </item>
    <!-- 一级菜单 -->
    <item
        android:id="@+id/chioce"
        android:title="@string/chioce">

        <!-- 二级菜单 -->
        <menu>
            <item
                android:id="@+id/create1"
                android:title="@string/create">
            </item>
            <item
                android:id="@+id/open1"
                android:title="@string/open">
            </item>
        </menu>
    </item>

</menu>

(3)values目录下的string.xml文件中:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Menu_submenu</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="file">文件</string>
    <string name="create">新建</string>
    <string name="open">打开</string>
    <string name="chioce">选项</string>

</resources>

(3)MainActivity.java代码如下:

package com.example.menu_submenu;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
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);
		return true;
	}

	/*
	 * 处理一级菜单
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.file:
			Toast.makeText(MainActivity.this, "一级菜单file..", Toast.LENGTH_SHORT)
					.show();
			break;
		case R.id.chioce:
			Toast.makeText(MainActivity.this, "一级菜单chioce..",
					Toast.LENGTH_SHORT).show();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	/*
	 * 处理二级菜单的
	 */
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case R.id.create1:
			Toast.makeText(MainActivity.this, "处理二级菜单create1..",
					Toast.LENGTH_SHORT).show();
			break;
		case R.id.open1:
			Toast.makeText(MainActivity.this, "处理二级菜单create1..",
					Toast.LENGTH_SHORT).show();
			break;
		case R.id.create:
			Toast.makeText(MainActivity.this, "处理二级菜单create..",
					Toast.LENGTH_SHORT).show();
			break;
		case R.id.open:
			Toast.makeText(MainActivity.this, "处理二级菜单open..",
					Toast.LENGTH_SHORT).show();
			break;

		default:
			break;
		}
		return super.onMenuItemSelected(featureId, item);
	}
}

技术分享

Android学习笔记之Menu一级菜单、二级菜单的使用

标签:android学习笔记之menu一级菜单   menu一级菜单二级菜单的使用   

原文地址:http://blog.csdn.net/xlgen157387/article/details/42804643

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!