menu菜单

本文详细介绍了Android中四种菜单的使用,包括上下文菜单、系统菜单、PopupMenu菜单以及重点讲解的PopupWindow菜单。同时,提到了在实现这些菜单时,需要在res/anim配置动画效果,在values/styles中进行样式设置,并在res/menu创建数据源。
摘要由CSDN通过智能技术生成

1.上下文菜单

    @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(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.white:
                Toast.makeText(this, "白色", Toast.LENGTH_SHORT).show();
                break;
            case R.id.black:
                Toast.makeText(this, "黑色", Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onContextItemSelected(item);
    }

2.系统菜单

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.menu,menu);
	return super.onCreateOptionsMenu(menu);
   @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.white:
                Toast.makeText(this, "白色", Toast.LENGTH_SHORT).show();
                break;
            case R.id.black:
                Toast.makeText(this, "黑色", Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

3.PopupMenu菜单

  PopupMenu popupMenu = new PopupMenu(Main2Activity.this,tv_show1);

                popupMenu.inflate(R.menu.menu);

                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.white:
                                Toast.makeText(Main2Activity.this, "白色", Toast.LENGTH_SHORT).show();
                                break;
                            case R.id.black:
                                Toast.makeText(Main2Activity.this, "黑色", Toast.LENGTH_SHORT).show();
                                break;
                        }

                        return false;
                    }
                });


                popupMenu.show();

4.popuwindow菜单(重要)

                //创建窗体
                PopupWindow popupWindow=new PopupWindow(Main3Activity.this);

                View inflate = LayoutInflater.from(Main3Activity.this).inflate(R.layout.activity_main2, null);

                //视图放入窗体
                popupWindow.setContentView(inflate);
                //窗体设置宽度
                popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
                //窗体设置高度
                popupWindow.setHeight(300);
                //设置用户点击窗体以外部分消失
                popupWindow.setOutsideTouchable(true);
                //设置窗体动画效果
                popupWindow.setAnimationStyle(R.style.pop);
                //设置背光
                WindowManager.LayoutParams attributes = getWindow().getAttributes();
                attributes.alpha=0.1f;
                getWindow().setAttributes(attributes);

                //设置监听,背光结束
                popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        WindowManager.LayoutParams attributes1 = getWindow().getAttributes();
                        attributes1.alpha=1.0f;
                        getWindow().setAttributes(attributes1);
                    }
                });


//                popupWindow.showAsDropDown(iv_1,0,0);
                //设置父布局居中
                View inflate1 = LayoutInflater.from(Main3Activity.this).inflate(R.layout.activity_main3, null);
                popupWindow.showAtLocation(inflate1,Gravity.CENTER,0,0);

1.res/anim 动画效果

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000">

    <translate android:fromYDelta="-500" 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="1000">

    <translate android:fromXDelta="0" android:toXDelta="500"></translate>
    <alpha android:fromAlpha="1" android:toAlpha="0.1"></alpha>

</set>

2.设置values/styles

    <style name="pop" parent="android:Animation">
        <item name="android:windowEnterAnimation">@anim/pop</item>
        <item name="android:windowExitAnimation"> @anim/pop_out</item>
    </style>

完成以上需要创建需数据源res/menu

<?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/white" android:title="白色"></item>
    <item android:id="@+id/black" android:title="黑色"></item>

    <item android:id="@+id/blue" android:title="蓝色" android:icon="@mipmap/ic_launcher" app:showAsAction="always"></item>

    <item android:id="@+id/er" android:title="二级菜单">
        <menu>
        <item android:id="@+id/one" android:title="白色"></item>
        <item android:id="@+id/two" android:title="黑色"></item>
        </menu>
    </item>

</menu>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值