PopupWindow和PopupMenu

1、PopupWindow可以创建类似对话窗风格的窗口,只需如下两步即可:
一调用PopupWindow的构造器构造PopupWindow对象
二调用PopupWindow的showAsDropDown(v)将PopupWindow作为v组件的下拉组件显示出来;或调用showAtLocation()在指定位置显示出来,使用比较简单,示例代码如下:


// 参数:窗体的视图,宽度,高度
View view = getLayoutInflater().inflate(R.layout.popupwindowlayout, null);
PopupWindow popupwindow = new PopupWindow(view, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

popupwindow.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.ic_menu_camera));
popupwindow.setAnimationStyle(android.R.style.DeviceDefault_ButtonBar_AlertDialog);// 设置动画
popupwindow.getBackground().setAlpha(70);// 设置透明度
popupwindow.setOutsideTouchable(true);// 点击边上消失
popupwindow.setFocusable(true);// 当前获取到焦点
popupwindow.setTouchable(true);// 可以被触摸
popupwindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);// 软键盘弹出

// 显示(相对于V试图来说,显示在下方,偏移量为00)
popupwindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
// 相对于某个控件的正左下方(无偏移量)
// popupwindow.showAsDropDown(v);
// 相对于某个控件的正左下方(无偏移量)
// popupwindow.showAsDropDown(v, 50, 50);

R.layout.popup为popupwindowlayout.xml为PopupWindow设计好的布局文件

2、PopupMenu弹出菜单
使用步骤如下:
一创建PopupMenu对象
二加载菜单资源
三为为菜单项的单击事件绑定监听器
四调用PopupMenu的show()方法弹出菜单
使用dismiss()方法关闭菜单
下面是一个简单示例:

<code class="java"><span style="font-size: 18px;"><span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">onPopupButtonClick</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(View button)</span>
</span>{
    <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 创建PopupMenu对象,button为触发该PopupMenu的组件,这里是一个按钮,由点击事件触发</span>
    popup = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> PopupMenu(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>, button);
    popup.
    <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 将R.menu.popup_menu菜单资源加载到popup菜单中</span>
    getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
    <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 为popup菜单的菜单项单击事件绑定事件监听器</span>
    popup.setOnMenuItemClickListener(
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> PopupMenu.OnMenuItemClickListener()
        {
            <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span>
            <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">boolean</span> <span class="hljs-title">onMenuItemClick</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(MenuItem item)</span>
            </span>{
                <span class="hljs-keyword" style="color: rgb(0, 0, 136);">switch</span> (item.getItemId())
                {
                    <span class="hljs-comment" style="color: rgb(136, 0, 0);">// exit为popup_menu中定义的菜单项的id</span>
                    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">case</span> R.id.exit:
                        popup.dismiss();
                        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">break</span>;
                    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">default</span>:
                        <span class="hljs-comment" style="color: rgb(136, 0, 0);">// </span>
                }
                <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">true</span>;
            }
        });
    popup.show();
}</span></code>

主界面只有一个按钮button,为该按钮绑定了单击事件监听器,该事件触发PopupMenu的创建,上述代码只有按钮的监听器回调函数
为PopupMenu定义的菜单布局为popup_menu.xml:

<code class="xml"><span style="font-size: 18px;"><span class="hljs-pi" style="color: rgb(0, 102, 102);"><?xml version="1.0" encoding="utf-8"?></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">menu</span> <span class="hljs-attribute" style="color: rgb(102, 0, 102);">xmlns:android</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"http://schemas.android.com/apk/res/android"</span>></span>
    <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">item</span>
        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:id</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"@+id/search"</span>
        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:icon</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"@android:drawable/ic_menu_search"</span>
        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:title</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"MenuItem1"</span> /></span>
    <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">item</span>
        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:id</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"@+id/add"</span>
        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:icon</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"@android:drawable/ic_menu_add"</span>
        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:title</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"MenuItem2"</span> /></span>
    <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">item</span>
        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:id</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"@+id/edit"</span>
        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:icon</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"@android:drawable/ic_menu_edit"</span>
        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:title</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"MenuItem3"</span>></span>
        <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">menu</span>></span>
            <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">item</span>
                <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:id</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"@+id/copy"</span>
                <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:title</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"SubMenuItem1"</span> /></span>
            <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">item</span>
                <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:id</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"@+id/cut"</span>
                <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:title</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"SubMenuItem2"</span> /></span>            
            <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">item</span>
                <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:id</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"@+id/paste"</span>
                <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:title</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"SubMenuItem3"</span> /></span>            
        <span class="hljs-tag" style="color: rgb(0, 102, 102);"></<span class="hljs-title" style="color: rgb(0, 0, 136);">menu</span>></span>
    <span class="hljs-tag" style="color: rgb(0, 102, 102);"></<span class="hljs-title" style="color: rgb(0, 0, 136);">item</span>></span>
    <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">item</span>
        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:id</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"@+id/exit"</span>
        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:title</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"ExitItem"</span> /></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102);"></<span class="hljs-title" style="color: rgb(0, 0, 136);">menu</span>></span></span></code>


文/CoderClock(简书作者)
原文链接:http://www.jianshu.com/p/6ce37c686315
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值