1.上下文菜单
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.menu, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.white:
Toast.makeText(this, "白色", Toast.LENGTH_SHORT).show();
break;
case R.id.black:
Toast.makeText(this, "黑色", Toast.LENGTH_SHORT).show();
break;
}
return super.onContextItemSelected(item);
}
2.系统菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.white:
Toast.makeText(this, "白色", Toast.LENGTH_SHORT).show();
break;
case R.id.black:
Toast.makeText(this, "黑色", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
3.PopupMenu菜单
PopupMenu popupMenu = new PopupMenu(Main2Activity.this,tv_show1);
popupMenu.inflate(R.menu.menu);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.white:
Toast.makeText(Main2Activity.this, "白色", Toast.LENGTH_SHORT).show();
break;
case R.id.black:
Toast.makeText(Main2Activity.this, "黑色", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
popupMenu.show();
4.popuwindow菜单(重要)
PopupWindow popupWindow=new PopupWindow(Main3Activity.this);
View inflate = LayoutInflater.from(Main3Activity.this).inflate(R.layout.activity_main2, null);
popupWindow.setContentView(inflate);
popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(300);
popupWindow.setOutsideTouchable(true);
popupWindow.setAnimationStyle(R.style.pop);
WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.alpha=0.1f;
getWindow().setAttributes(attributes);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams attributes1 = getWindow().getAttributes();
attributes1.alpha=1.0f;
getWindow().setAttributes(attributes1);
}
});
View inflate1 = LayoutInflater.from(Main3Activity.this).inflate(R.layout.activity_main3, null);
popupWindow.showAtLocation(inflate1,Gravity.CENTER,0,0);
1.res/anim 动画效果
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000">
<translate android:fromYDelta="-500" android:toYDelta="0"></translate>
<alpha android:fromAlpha="0.1" android:toAlpha="1"></alpha>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000">
<translate android:fromXDelta="0" android:toXDelta="500"></translate>
<alpha android:fromAlpha="1" android:toAlpha="0.1"></alpha>
</set>
2.设置values/styles
<style name="pop" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/pop</item>
<item name="android:windowExitAnimation"> @anim/pop_out</item>
</style>
完成以上需要创建需数据源res/menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/white" android:title="白色"></item>
<item android:id="@+id/black" android:title="黑色"></item>
<item android:id="@+id/blue" android:title="蓝色" android:icon="@mipmap/ic_launcher" app:showAsAction="always"></item>
<item android:id="@+id/er" android:title="二级菜单">
<menu>
<item android:id="@+id/one" android:title="白色"></item>
<item android:id="@+id/two" android:title="黑色"></item>
</menu>
</item>
</menu>