android:layout_width="match_parent" android:layout_height="match_parent">
//showAsAction属性值:always直接在标题栏显示
never:不显示
with Text:控制图标和文本一起显示
ifRoom:有空间就显示
//icon设置为图片,默认不显示文字
一:OptionMenu
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//覆盖onCreateOptionsMenu方法
//覆盖onOptionsItemSelected方法
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.option,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.option_1:
Toast.makeText(MainActivity.this,"shezhi",Toast.LENGTH_LONG).show();
}
return super.onOptionsItemSelected(item);
}
}
二:ContextMenu
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 1.注册
registerForContextMenu(findViewById(R.id.menu));
// 2创建 覆盖onCreateContextMenu
// 3.菜单项操作 覆盖onContextItemSelected
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.option,menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.option_1:
Toast.makeText(MainActivity.this,"menu",Toast.LENGTH_LONG).show();
break;
}
return super.onContextItemSelected(item);
}
}
第二种效果:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 为按钮设置上下文操作模式
// 1 实现ActionMode CallBack
// 2 在view的长按事件中去启动上下文操作模式
findViewById(R.id.menu).setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
startActionMode(cb);
return false;
}
});
}
ActionMode.Callback cb = new ActionMode.Callback() {
// 创建,在启动上下文操作模式(startActionMode(Callback))时调用
@Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
getMenuInflater().inflate(R.menu.option,menu);
return true;
}
// 在创建方法后进行调用
@Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.option_1:
Toast.makeText(MainActivity.this,"menu",Toast.LENGTH_LONG).show();
}
return false;
}
// 上下文操作模式结束时被调用
@Override
public void onDestroyActionMode(ActionMode actionMode) {
}
};
}
三:PopupMenu
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button)findViewById(R.id.menu);
// 演示popuMenu
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 1 实例化popuMenu对象(参数2:被锚定的view)
PopupMenu menu = new PopupMenu(MainActivity.this,button);
// 2 加载菜单资源:利用Menuinflater将Menu资源加载到PopupMenu.getMenu()所返回的Menu对象中
// 将R.menu.xx对应的菜单资源加载到弹出式菜单中
menu.getMenuInflater().inflate(R.menu.option,menu.getMenu());
// 3 为popupMenu设置点击监听器
menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.option_1:
Toast.makeText(MainActivity.this,"menu",Toast.LENGTH_LONG).show();
}
return false;
}
});
// 4千万不要忘记这一步
menu.show();
}
});
}
}