popupWindow的简单使用

1、PopupWindow与AlertDialog的区别

最关键的区别是AlertDialog不能指定显示位置,只能默认显示在屏幕最中间(当然也可以通过设置WindowManager参数来改变位置)。而PopupWindow是可以指定显示位置的,随便哪个位置都可以,更加灵活。
有关Dialog的相关知识,大家可以参考我的系列博客: 《详解Dialog(一)——基础元素构建》

2、PopupWindow的相关函数

(1)、构造函数:

[java]  view plain  copy
  1. //方法一:  
  2. public PopupWindow (Context context)  
  3. //方法二:  
  4. public PopupWindow(View contentView)  
  5. //方法三:  
  6. public PopupWindow(View contentView, int width, int height)  
  7. //方法四:  
  8. public PopupWindow(View contentView, int width, int height, boolean focusable)  
首要注意:看这里有四个构造函数,但要生成一个PopupWindow最基本的三个条件是一定要设置的:View contentView,int width, int height ;少任意一个就不可能弹出来PopupWindow!!!!
所以,如果使用方法一来构造PopupWindow,那完整的构造代码应该是这样的:

[java]  view plain  copy
  1. View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null);  
  2. PopupWindwo popWnd = PopupWindow (context);  
  3. popWnd.setContentView(contentView);  
  4. popWnd.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);  
  5. popWnd.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);  
有关为什么一定要设置width和height的原因,我们后面会讲,这里说一下为什么样强制设置contentView;很简单的原因是因为PopupWindow没有默认布局,它不会像AlertDialog那样只setTitle,就能弹出来一个框。PopupWindow是没有默认布局的,它的布局只有通过我们自己设置才行。由于方法三中,含有了这三个必备条件,不用单独设置contentview或者width、height,所以构造方法三是用的最多的一个构造方法。
最后,方法四中的focusable变量不是必须的,有关它的方法和意义,我们会在下一篇中细讲。

(2)显示函数

显示函数主要使用下面三个:

[java]  view plain  copy
  1. //相对某个控件的位置(正左下方),无偏移  
  2. showAsDropDown(View anchor):  
  3. //相对某个控件的位置,有偏移;xoff表示x轴的偏移,正值表示向左,负值表示向右;yoff表示相对y轴的偏移,正值是向下,负值是向上;  
  4. showAsDropDown(View anchor, int xoff, int yoff):  
  5. //相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移  
  6. showAtLocation(View parent, int gravity, int x, int y):  
这里有两种显示方式:
1、显示在某个指定控件的下方
showAsDropDown(View anchor):
showAsDropDown(View anchor, int xoff, int yoff);
2、指定父视图,显示在父控件的某个位置(Gravity.TOP,Gravity.RIGHT等)
showAtLocation(View parent, int gravity, int x, int y);

(3)、其它函数

[java]  view plain  copy
  1. public void dismiss()  
  2. //另外几个函数,这里不讲其意义,下篇细讲  
  3. public void setFocusable(boolean focusable)  
  4. public void setTouchable(boolean touchable)  
  5. public void setOutsideTouchable(boolean touchable)  
  6. public void setBackgroundDrawable(Drawable background)  
public class MainActivity extends AppCompatActivity{

    private Button button;
    private PopupWindow popupWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View view = LayoutInflater.from(this).inflate(R.layout.popuwindow,null);
        popupWindow = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT,false);
        initView();
    }

    private void initView() {
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onClick(View view) {
                if (popupWindow.isShowing()){
                    popupWindow.dismiss();
                }else {
                    //显示在父级控件的下方
                    popupWindow.showAtLocation(view,Gravity.BOTTOM,0,0);
                    //显示在本控件的下方
//                    popupWindow.showAsDropDown(view,0,0,Gravity.BOTTOM);
                }
            }
        });
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值