main.xml
<item
android:id="@+id/sms"
android:icon="@drawable/sms"
android:orderInCategory="100"
android:showAsAction="never"
android:title="短信">
<menu>
<item
android:id="@+id/smsfamily"
android:icon="@drawable/smsalt"
android:orderInCategory="100"
android:showAsAction="never"
android:title="家庭短信"/>
<item
android:id="@+id/smscompany"
android:icon="@drawable/twitteralt"
android:orderInCategory="100"
android:showAsAction="never"
android:title="公司短信"/>
</menu>
</item>
<item
android:id="@+id/camera"
android:icon="@drawable/camera"
android:orderInCategory="100"
android:showAsAction="never"
android:title="照相机"/>
<item
android:id="@+id/email"
android:icon="@drawable/email"
android:orderInCategory="100"
android:showAsAction="never"
android:title="邮箱"/>
<item
android:id="@+id/browser"
android:icon="@drawable/browser"
android:orderInCategory="100"
android:showAsAction="never"
android:title="浏览器"/>
<item
android:id="@+id/calculator"
android:icon="@drawable/calculator"
android:orderInCategory="100"
android:showAsAction="never"
android:title="计算器"/>
<item
android:id="@+id/clock"
android:icon="@drawable/clock"
android:orderInCategory="100"
android:showAsAction="never"
android:title="时钟"/>
<item
android:id="@+id/maps"
android:icon="@drawable/maps"
android:orderInCategory="100"
android:showAsAction="never"
android:title="地图"/>
context.xml
<group
android:id="@+id/g1"
>
<item
android:id="@+id/sms"
android:icon="@drawable/sms"
android:orderInCategory="100"
android:showAsAction="never"
android:title="短信">
<menu>
<item
android:id="@+id/smsfamily"
android:icon="@drawable/smsalt"
android:orderInCategory="100"
android:showAsAction="never"
android:title="家庭短信"/>
<item
android:id="@+id/smscompany"
android:icon="@drawable/twitteralt"
android:orderInCategory="100"
android:showAsAction="never"
android:title="公司短信"/>
</menu>
</item>
<item
android:id="@+id/camera"
android:icon="@drawable/camera"
android:orderInCategory="100"
android:showAsAction="never"
android:title="照相机"/>
</group>
<group android:id="@+id/g2">
<item
android:id="@+id/email"
android:icon="@drawable/email"
android:orderInCategory="100"
android:showAsAction="never"
android:title="邮箱"/>
<item
android:id="@+id/browser"
android:icon="@drawable/browser"
android:orderInCategory="100"
android:showAsAction="never"
android:title="浏览器"/>
</group>
<group android:id="@+id/g3">
<item
android:id="@+id/calculator"
android:icon="@drawable/calculator"
android:orderInCategory="100"
android:showAsAction="never"
android:title="计算器"/>
<item
android:id="@+id/clock"
android:icon="@drawable/clock"
android:orderInCategory="100"
android:showAsAction="never"
android:title="时钟"/>
<item
android:id="@+id/maps"
android:icon="@drawable/maps"
android:orderInCategory="100"
android:showAsAction="never"
android:title="地图"/>
</group>
MainActicity.java
public class MainActivity extends Activity {
private TextView tv;
int n = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
registerForContextMenu(tv);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
setIconVisible(menu);
return true;
}
// 目的就是让所有的图标显示出来
private void setIconVisible(Menu menu) {
try {
Class clazz = Class
.forName("com.android.internal.view.menu.MenuBuilder");
Method m = clazz.getDeclaredMethod("setOptionalIconsVisible",
boolean.class);
m.setAccessible(true);
m.invoke(menu, true);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, item.getTitle(), 0).show();
return super.onOptionsItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// 加载xml文件,创建上下文菜单
getMenuInflater().inflate(R.menu.context, menu);
setIconVisible(menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
Toast.makeText(this, item.getTitle(), 0).show();
return super.onContextItemSelected(item);
}
}