PopupMenu、PopupWindow的使用

PopMenu的使用

        PopupMenu popupMenu = new PopupMenu(this, view);
        getMenuInflater().inflate(R.menu.main, popupMenu.getMenu());
        popupMenu.show();
  • 这里需要注意的是:填充器是特定的填充器,不能使用普通的填充器
  • 里面填充过的布局文件必须是menu文件夹下main特定的
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>
    <item
        android:id="@+id/action_btn"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>
    <item
        android:id="@+id/xxx"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>
</menu>

PopupWindow的使用心得

从构造方法中我们可以看出,要使用PopupWindow()就必须要设置三个属性

  • contentView  要显示的布局
    
  • width  设置具体的大小,不设置传入
    

ViewGroup.LayoutParams.WRAP_CONTENT

  • height  和宽度一样,必须要设置
    
下面是一个简单的使用
    private void showPopWindow() {
        View contentView = View.inflate(this, R.layout.menu, null);
        PopupWindow popupWindow = new PopupWindow(this);
        popupWindow.setContentView(contentView);
        Button btn1 = (Button) contentView.findViewById(R.id.btn_1);
        btn1.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "OneX", 1).show();
            }
        });
        
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        
        
//      popupWindow.showAsDropDown(btnAmShowmenu);
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAtLocation((View) btnAmShowmenu.getParent(), Gravity.BOTTOM, 0, 0);

    }

context_menu_enter.xml 新建的anim文件夹,里面对应相应的文件

<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android">  
  
    <translate  
        android:duration="@android:integer/config_shortAnimTime"  
        android:fromXDelta="0"  
        android:fromYDelta="100%p"  
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
        android:toXDelta="0"  
        android:toYDelta="0"/>  
  
</set> 
<style name="contextMenuAnim" parent="@android:style/Animation.Activity">          <item name="android:windowEnterAnimation">@anim/context_menu_enter</item>  
    <item name="android:windowExitAnimation">@anim/context_menu_exit</item>  
</style> 

1、setTouchable(boolean touchable)

设置PopupWindow是否响应touch事件,默认是true,如果设置为false,即会是下面这个结果:(所有touch事件无响应,包括点击事件)

2、setFocusable(boolean focusable)

该函数的意义表示,PopupWindow是否具有获取焦点的能力,默认为False。一般来讲是没有用的,因为普通的控件是不需要获取焦点的,而对于EditText则不同,如果不能获取焦点,那么EditText将是无法编辑的。
所以,我们在popuplayout.xml最底部添加一个EditText,分别演示两段不同的代码,即分别将setFocusable设置为false和设置为true;

3、setOutsideTouchable(boolean touchable)

这个函数的意义,就是指,PopupWindow以外的区域是否可点击,即如果点击PopupWindow以外的区域,PopupWindow是否会消失。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值