android Menu 菜单使用总结

首先建议阅读官方提供的API阅读:

http://developer.android.com/guide/topics/ui/menus.html#context-menu

阅读之后简单写其中的某些功能,先贴图:

android Menu 菜单使用总结_第1张图片android Menu 菜单使用总结_第2张图片

android Menu 菜单使用总结_第3张图片android Menu 菜单使用总结_第4张图片

android Menu 菜单使用总结_第5张图片


第一步:

android Menu 菜单使用总结_第6张图片

相关代码:

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

    <item
        android:id="@+id/item1"
        android:icon="@drawable/about_icon"
        android:showAsAction="always"
        android:title="item1">
        <menu>
            <item
                android:id="@+id/create_new"
                android:title="new "/>
            <item
                android:id="@+id/open"
                android:title="open"/>
        </menu>

        <item/>
        <item
            android:id="@+id/item2"
            android:icon="@drawable/abs__ic_search"
            android:showAsAction="ifRoom"
            android:title="item2">

            <!-- menu group -->
            <group
                android:id="@+id/group"
             >
                <item
                    android:id="@+id/groupitem1"
                    android:showAsAction="ifRoom"
                    android:title="groupitem1"/>
                <item
                    android:id="@+id/groupitem2"
                    android:showAsAction="ifRoom"
                    android:title="groupitem2"/>
            </group>

            <item/>
            <item
                android:id="@+id/item3"
                android:icon="@drawable/ad_def_logo"
                android:showAsAction="always"
                android:title="item3"/>
            <item
                android:id="@+id/item4"
                android:icon="@drawable/abs__ic_voice_search"
                android:showAsAction="always"
                android:title="item4"/>
            <item
                android:id="@+id/item5"
                android:icon="@drawable/abs__ic_voice_search"
                android:showAsAction="never"
                android:title="item5"/>
        </item>
    </item>

</menu>
第二步:

代码中的实现:

package com.jiaruihuademo.testmenu;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.Toast;

/**
 * 
 * menu 的简单使用
 * 
 * @author JiaRH
 * 
 * @date 2013-12-30 上午9:55:29
 */
@SuppressLint("NewApi")
public class MainActivity extends Activity implements OnMenuItemClickListener,
		OnCreateContextMenuListener, OnItemClickListener {

	private ListView listview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		listview = (ListView) findViewById(R.id.listView1);
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < 20; i++) {

			list.add(i + "");
		}
		listview.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, list));

		listview.setOnItemClickListener(this);

	}

	/**
	 * 创建popviewMenu
	 * 
	 * @param v
	 */
	@SuppressLint("NewApi")
	public void showpop(View v) {
		PopupMenu popup = new PopupMenu(this, v);
		MenuInflater inflater = popup.getMenuInflater();
		inflater.inflate(R.menu.main, popup.getMenu());
		popup.setOnMenuItemClickListener(this);
		popup.show();

	}

	@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;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.item1:
			Toast.makeText(this, "you have clicked 1 ", 0).show();
			break;
		case R.id.item2:
			Toast.makeText(this, "you have clicked 2 ", 0).show();
			break;
		case R.id.item3:
			Toast.makeText(this, "you have clicked 3 ", 0).show();
			break;
		case R.id.item4:
			Toast.makeText(this, "you have clicked 4 ", 0).show();
			break;

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

	/**
	 * 响应popmenu的点击事件
	 */
	@Override
	public boolean onMenuItemClick(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.item1:
			Toast.makeText(this, "you have clicked 1 ", 0).show();
			break;
		case R.id.item2:
			Toast.makeText(this, "you have clicked 2 ", 0).show();
			break;
		case R.id.item3:
			Toast.makeText(this, "you have clicked 3 ", 0).show();
			break;
		case R.id.item4:
			Toast.makeText(this, "you have clicked 4 ", 0).show();
			break;

		default:
			break;
		}
		return false;
	}

	/**
	 * listview响应事件
	 */
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		registerForContextMenu(view);

	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();
		switch (item.getItemId()) {
		case R.id.item1:
			Toast.makeText(this, "you have clicked 1 ", 0).show();
			return true;
		case R.id.item2:
			Toast.makeText(this, "you have clicked 2 ", 0).show();
			return true;
		case R.id.item3:
			Toast.makeText(this, "you have clicked 3 ", 0).show();
			return true;
		case R.id.item4:
			Toast.makeText(this, "you have clicked 4 ", 0).show();
			return true;
		default:
			return super.onContextItemSelected(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"
    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:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/showpop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="16dp"
        android:text="show popmenu"
        android:onClick="showpop" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/showpop"
        android:layout_below="@+id/showpop"
        android:layout_marginTop="40dp" >
    </ListView>

</RelativeLayout>

备注;

我使用的



    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />


你可能感兴趣的:(android,技术,总结,布局,menu)