1.系统菜单OptionsMenu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/red" android:title="红色" ></item>
<item android:id="@+id/green" android:title="绿色"></item>
</menu>
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case R.id.red:
Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
break;
case R.id.green:
Toast.makeText(this, "绿色", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
2.上下文菜单ContextMenu
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.menu, menu);
}
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case R.id.red:
Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
break;
case R.id.green:
Toast.makeText(this, "绿色", Toast.LENGTH_SHORT).show();
break;
}
return super.onContextItemSelected(item);
}
3.PopPupWindow
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000">
<alpha android:fromAlpha="0.1" android:toAlpha="1"></alpha>
<translate android:fromYDelta="-300" android:toYDelta="0"></translate>
</set>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000">
<alpha android:fromAlpha="1" android:toAlpha="0.1"></alpha>
<translate android:fromYDelta="0" android:toYDelta="300"></translate>
</set>
public void btn(View view) {
final PopupWindow popupWindow = new PopupWindow(this);
View inflate = LayoutInflater.from(this).inflate(R.layout.pop_layout, null);
TextView textview = inflate.findViewById(R.id.pop_tv1);
textview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.alpha = 0.5f;
getWindow().setAttributes(attributes);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.alpha = 1f;
getWindow().setAttributes(attributes);
}
});
popupWindow.setContentView(inflate);
popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setOutsideTouchable(true);
popupWindow.setAnimationStyle(R.style.pop);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
View inflate1 = LayoutInflater.from(this).inflate(R.layout.pop_layout, null);
popupWindow.showAtLocation(inflate1, Gravity.CENTER,0,0);
}
registerForContextMenu(tv2);
tv2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(MainActivity.this,tv2);
popupMenu.inflate(R.menu.menu);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case R.id.red:
tv.setTextColor(Color.parseColor("#556B2F"));
break;
case R.id.green:
tv.setTextColor(Color.parseColor("#00FF7F"));
break;
}
return false;
}
});
popupMenu.show();
}
});