菜单Menu(选择,上下,侧滑)
2019-02-21 本文已影响0人
君袅
选择菜单
一.OptionMenu
/创建选项菜单
/**
* 分组:
* <group android:id="@+id/group1">
*
* Item条目:
* android:id="@+id/item1" Id
* android:title="增加" 标题
* android:icon="@android:drawable/ic_delete" 图标
* app:showAsAction="always" 显示方式 always外部 never内部 ifRoom空间
* android:orderInCategory="200" 排列方式 值越小越靠前显示
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//方式一:通过xml文件创建菜单
getMenuInflater().inflate(R.menu.layout_menu,menu);
//方式二:通过Java代码添加
MenuItem item1 = menu.add(1, 100, 100, "全选");
item1.setIcon(android.R.drawable.ic_dialog_map);
item1.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(1,200,200,"反选");
return super.onCreateOptionsMenu(menu);
}
//选项菜单的选中监听处理
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.item1:
Toast.makeText(MainActivity.this,"item:"+item.getTitle(),Toast.LENGTH_SHORT).show();
break;
case R.id.item2:
Toast.makeText(MainActivity.this,"item:"+item.getTitle(),Toast.LENGTH_SHORT).show();
break;
case R.id.item3:
Toast.makeText(MainActivity.this,"item:"+item.getTitle(),Toast.LENGTH_SHORT).show();
break;
case R.id.item4:
Toast.makeText(MainActivity.this,"item:"+item.getTitle(),Toast.LENGTH_SHORT).show();
break;
case 100:
Toast.makeText(MainActivity.this,"item:"+item.getTitle(),Toast.LENGTH_SHORT).show();
break;
case 200:
Toast.makeText(MainActivity.this,"item:"+item.getTitle(),Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
}
上下文菜单
//3.给对应的View控件设置上下文菜单
registerForContextMenu(btn3);
registerForContextMenu(btn1);
//1.创建上下文菜单
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
//根据不同View设置不同上下文菜单
if (v.getId() == R.id.btn1){
//方式一:通过xml文件创建菜单
getMenuInflater().inflate(R.menu.context_menu1, menu);
}else{
//方式二:通过Java代码创建菜单
menu.add(1, 100, 100, "全选");
menu.add(1, 200, 200, "反选");
}
}
//2.设置上下文菜单的监听事件
@Override
public boolean onContextItemSelected(MenuItem item) {
//获取ItemId进行判断
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
break;
case R.id.item2:
Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
break;
case R.id.item3:
Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
break;
case R.id.item4:
Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
break;
}
rdeturn super.onContextItemSelected(item);
侧滑菜单
//1.设置图标不显示处理
nv.setItemIconTintList(null);
//2.设置菜单点击处理
nv.setNavigationItemSelectedListener(this);
//3.设置侧滑菜单图片更改
View headerView = nv.getHeaderView(0);
ImageView img = headerView.findViewById(R.id.img);
img.setImageResource(android.R.drawable.ic_delete);
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "更换头像", Toast.LENGTH_SHORT).show();
}
});
//对侧滑菜单的监听处理(开、关监听)
dl.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(@NonNull View view, float v) {
}
@Override
public void onDrawerOpened(@NonNull View view) {
Toast.makeText(MainActivity.this, "开启", Toast.LENGTH_SHORT).show();
}
@Override
public void onDrawerClosed(@NonNull View view) {
Toast.makeText(MainActivity.this, "关闭", Toast.LENGTH_SHORT).show();
}
@Override
public void onDrawerStateChanged(int i) {
}
});
//设置侧滑菜单的开关处理
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,dl,toolBar,R.string.app_name,R.string.app_name);
dl.addDrawerListener(toggle);
toggle.syncState();
}
//侧滑菜单的点击事件 需要手动实验接口 NavigationView.OnNavigationItemSelectedListener 生成如下方法
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
//menuItem.setChecked(true);
switch (menuItem.getItemId()) {
case R.id.item1:
Toast.makeText(MainActivity.this, menuItem.getTitle(), Toast.LENGTH_SHORT).show();
break;
case R.id.item2:
Toast.makeText(MainActivity.this, menuItem.getTitle(), Toast.LENGTH_SHORT).show();
break;
case R.id.item3:
Toast.makeText(MainActivity.this, menuItem.getTitle(), Toast.LENGTH_SHORT).show();
break;
case R.id.item4:
Toast.makeText(MainActivity.this, menuItem.getTitle(), Toast.LENGTH_SHORT).show();
break;
}
return true;
}
//通过代码控制侧滑菜单的开启和关闭
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
dl.openDrawer(Gravity.LEFT);
break;
case R.id.btn2:
dl.closeDrawer(Gravity.LEFT);
break;
}