欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Menu菜单的几种分类

程序员文章站 2022-05-27 08:29:50
##Menu1.OptionsMenu:出现在系统栏2.ContextMenu:出现屏幕正*(注册控件 长按来触发)3.PopupMenu:出现在指定控件的下方(控件写点击事件来触发 可以有多个)4.PopupWindow特点:自定义布局,指定出现的位置位置:1.出现在某个控件的下方2.出现在父容器的某个位置常用方法setContentView(自定义布局的View对象)2.setWidth();setHeight();3.showAtLocation(父容器view对象,Gra...

##Menu
1.OptionsMenu:出现在系统栏
2.ContextMenu:出现屏幕正*(注册控件 长按来触发)
3.PopupMenu:出现在指定控件的下方(控件写点击事件来触发 可以有多个)
4.PopupWindow
特点:自定义布局,指定出现的位置
位置:
1.出现在某个控件的下方
2.出现在父容器的某个位置
常用方法
setContentView(自定义布局的View对象)
2.setWidth();
setHeight();
3.showAtLocation(父容器view对象,Gravity.Bottom,0,0);
4.showAsDorpdown(子控件,0,0,Gravity.Bottom)
5.view.getBackground.setAlph(100);
popupweindow.dismiss();关闭

//系统菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu1,menu);
        return super.onCreateOptionsMenu(menu);
    }
    //子菜单点击事件
    @Override
    public boolean onOptionsItemSelected(@NonNull final MenuItem item) {
        switch (item.getItemId()){
            case R.id.menu:
                final PopupWindow popupWindow = new PopupWindow();
                View view2 = LayoutInflater.from(this).inflate(R.layout.iteam,null);
                //设置内容
                popupWindow.setContentView(view2);
                //指定宽高
                popupWindow.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
                popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
                popupWindow.showAtLocation(view2,Gravity.TOP,0,0);
                final TextView textView2 = view2.findViewById(R.id.iv_qun);
                textView2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this, ""+textView2.getText().toString(), Toast.LENGTH_SHORT).show();
                        popupWindow.dismiss();
                    }
                });
                break;
            case R.id.menu1:
                final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                View view= LayoutInflater.from(this).inflate(R.layout.item2,null);
                builder.setView(view);
                final EditText editText = view.findViewById(R.id.ed1);
                Button button = view.findViewById(R.id.bt1);
                builder.setTitle("增加菜品");
                builder.setIcon(R.mipmap.ic_launcher_round);
                final AlertDialog dialog = builder.create();
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String string = editText.getText().toString();
                        HashMap<String,String> map = new HashMap<>();
                        map.put("title",string);
                        list.add(map);
                        dialog.dismiss();
                    }
                });
                dialog.show();
                break;
            case R.id.menu2:
                AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
                builder1.setTitle("删除操作");
                builder1.setMessage("确定要删除删除?");
                builder1.setIcon(R.mipmap.ic_launcher);

                builder1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        list.remove(0);
                        myAdapter.notifyDataSetChanged();
                    }
                });
                builder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
                    }
                });
                AlertDialog dialog1 = builder1.create();
                dialog1.show();
                break;
            case R.id.menu3:
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,Main2Activity.class);
                intent.putExtra("name","我是Activity1");
                startActivity(intent);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
     //上下文菜单
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        getMenuInflater().inflate(R.menu.menu1,menu);
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        Toast.makeText(this, ""+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
        return super.onContextItemSelected(item);
    }

    private void initDates() {
        HashMap<String,String> map = new HashMap<>();
        map.put("stage_id","1");
        map.put("limit","20");
        map.put("page","1");
        new MyThread(url,map).start();
    }
 //popupMenu
    public void popupMenu(View view) {
        PopupMenu menu = new PopupMenu(MainActivity.this,tv2);
        menu.inflate(R.menu.option);

        menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                Log.e("onMenuItemClick: ",menuItem.getTitle().toString());
                return false;
            }
        });
        menu.show();

    }
//popupwindow
public void popupWindow(View view) {
        final PopupWindow popupWindow = new PopupWindow();
        View view1 = LayoutInflater.from(this).inflate(R.layout.item2_layout,null);
        popupWindow.setContentView(view1);

        popupWindow.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);

        //显示popupWindow   参数一:指定出现在哪个控件下
        //popupWindow.showAsDropDown(tv3,0,0, Gravity.BOTTOM);

        //显示在父容器布局中
        View parent = LayoutInflater.from(this).inflate(R.layout.activity_main,null);
        popupWindow.showAtLocation(parent, Gravity.BOTTOM,0,0);

//        final TextView lide = view1.findViewById(R.id.lide);
//        lide.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Log.e("onClick: ",lide.getText().toString());
//                popupWindow.dismiss();
//            }
//        });

        //透明效果
        view1.getBackground().setAlpha(100);
        final Button bu1 = view1.findViewById(R.id.bu1);
        Button bu2 = view1.findViewById(R.id.bu2);

        bu1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("onClick: ","点赞");
                popupWindow.dismiss();
            }
        });
        bu2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("onClick: ","取消");
                popupWindow.dismiss();
            }
        });



    }

本文地址:https://blog.csdn.net/SimpleJerry/article/details/107372597