常用的菜单
系统菜单OptionsMenu
<?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/one" android:title="一"></item>
<item android:id="@+id/two" android:title="二"></item>
<item android:id="@+id/three" android:title="二"></item>
<item android:id="@+id/four" android:icon="@mipmap/ic_launcher" app:showAsAction="always"></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.one:
Toast.makeText(this, "" + item.getTitle(), Toast.LENGTH_SHORT).show();
break;
case R.id.two:
Toast.makeText(this, "" + item.getTitle(), Toast.LENGTH_SHORT).show();
break;
case R.id.three:
Toast.makeText(this, "" + item.getTitle(), Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
上下文菜单ContextMenu
@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(@NonNull MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case R.id.one:
b1.setTextColor(Color.parseColor("#ff0000"));
break;
case R.id.two:
b1.setTextColor(Color.parseColor("#00ff00"));
break;
case R.id.three:
b1.setTextColor(Color.parseColor("#0000ff"));
break;
}
return super.onContextItemSelected(item);
}
b1 = (Button) findViewById(R.id.b1);
registerForContextMenu(b1);
弹出菜单
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(MainActivity.this, b2);
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.one:
b2.setTextColor(Color.parseColor("#ff0000"));
break;
case R.id.two:
b2.setTextColor(Color.parseColor("#00ff00"));
break;
case R.id.three:
b2.setTextColor(Color.parseColor("#0000ff"));
break;
}
return false;
}
});
popupMenu.show();
}
});
PopupWindow
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final PopupWindow popupWindow = new PopupWindow(MainActivity.this);
final View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.win_layout, null);
TextView textView = inflate.findViewById(R.id.t1);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
Toast.makeText(MainActivity.this, "hhh", Toast.LENGTH_SHORT).show();
}
});
popupWindow.setContentView(inflate);
popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
//出场动画
popupWindow.setAnimationStyle(R.style.pop);
popupWindow.setOutsideTouchable(true);
// popupWindow.showAsDropDown(b3,-500,-500);
View inflate1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main, null);
popupWindow.showAtLocation(inflate1, Gravity.CENTER,0,0);
//点击背景变暗
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 = 1.0f;
getWindow().setAttributes(attributes);
}
});
}
});
出场动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000">
<translate android:fromYDelta="-300" 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="2000">
<translate android:fromYDelta="0" android:toYDelta="300"/>
<alpha android:fromAlpha="1" android:toAlpha="0.1"/>
</set>
<style name="pop" parent="android:Animation">
<item name="android:windowEnterAnimation" >@anim/anim</item>
<item name="android:windowExitAnimation">@anim/animtwo</item>
</style>
番外 双击退出
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK){
if (System.currentTimeMillis() - time > 2000){
Toast.makeText(this, "再次点击将退出", Toast.LENGTH_SHORT).show();
time = System.currentTimeMillis();
return true;
}else {
finish();
}
}
return super.onKeyDown(keyCode, event);
}