菜单分为两种,OptionMenu(按住手机的MENU键就会出来),ContextMenu(在手机上长按1-2秒才会启动),按需求自取。下面是ContextOption的使用方法,OptionMenu的方法差不多。
1.main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/myLayout"
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="com.Harris.menuactivity01.MenuActivity1" >
<TextView
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按住这里修改背景颜色"
android:textSize="20sp" />
<TextView
android:id="@+id/txt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按住以修改字体大小"
android:textSize="20sp"
/>
</LinearLayout>
Java代码
package com.Harris.menuactivity01;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MenuActivity1 extends Activity {
private TextView txt1;
private TextView txt2;
LinearLayout myLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt1 = (TextView) findViewById(R.id.txt1);
txt2 = (TextView) findViewById(R.id.txt2);
myLayout = (LinearLayout)findViewById(R.id.myLayout);
registerForContextMenu(txt1);
registerForContextMenu(txt2);
}
protected static final int MENU_BACKGROUNDCOLOR = Menu.FIRST;
protected static final int MENU_SMALLSIZE = Menu.FIRST+1;
protected static final int MENU_LARGESIZE= Menu.FIRST+2;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_activity1, menu);
return true;
}
@Override
public void onCreateContextMenu(android.view.ContextMenu menu, android.view.View v,
android.view.ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v == txt1){
menu.add(0, MENU_BACKGROUNDCOLOR, 0, "更改颜色");
}else if(v == txt2){
//add方法里面第一个0代表选项组id,由0开始编号
menu.add(0, MENU_LARGESIZE, 1, "字体改大");
menu.add(0, MENU_SMALLSIZE, 2, "字体缩小");
}
};
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_BACKGROUNDCOLOR:
myLayout.setBackgroundColor(Color.BLUE);
break;
case MENU_LARGESIZE:
float a = txt1.getTextSize();
txt1.setTextSize(a+10f);
break;
case MENU_SMALLSIZE:
float b = txt2.getTextSize();
//这里要注意一开始的字体不能小于10
txt2.setTextSize(b-10f);
break;
default:
break;
}
return super.onContextItemSelected(item);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这里需要记住的方法有俩:OnCreateContextMenu和OnContextItemSelected,直接根据Eclipsed的提示来就好了。
快捷菜单会随着在屏幕上长按的位置的不同而有不同的内容,例如我们如果设置在文字组件上的快捷菜单与图片组件所显示的不同,
就必须在新增弹出功能之后再系统进行组件注册的动作。
registerForContextMenu(txt1);
类似的,OptionMenu对应方法有:OnCreateOptinMenu和OnOptionsItemSelected,相当好记。
相似点:
1.弹出的菜单的设置都是用menu.add()///这个menu是OnCreateContextMenu和OnCreateOptinMenu的一个参数。
2.判断用户选择的哪个都是用的item.getItemId()
完事。