美文网首页
[转]android PopupWindow在控件的各个方向上的

[转]android PopupWindow在控件的各个方向上的

作者: Amy_LuLu__ | 来源:发表于2019-05-28 11:03 被阅读0次

    转载地址:http://blog.csdn.net/dxj007/article/details/8026691

    这篇文章主要介绍了PopupWindow在控件的各个方向上的显示(上、下、左、右),主要用到PopupWindow的showAtLocation()方法。
    ①.在控件上方:

    private void showPopUp(View v) {      LinearLayout layout = new LinearLayout(this);      layout.setBackgroundColor(Color.GRAY);      TextView tv = new TextView(this);      tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));      tv.setText("I'm a pop -----------------------------!");      tv.setTextColor(Color.WHITE);      layout.addView(tv);       popupWindow = new PopupWindow(layout,120,120);      popupWindow.setFocusable(true);      popupWindow.setOutsideTouchable(true);      popupWindow.setBackgroundDrawable(new BitmapDrawable());       int[] location = new int[2];      v.getLocationOnScreen(location);       popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1]-popupWindow.getHeight());  } 
    

    ②.在控件的下方,只需修改最后一行代码即可,如:

    popupWindow.showAsDropDown(v);
    

    ③.在控件的左边,只需修改最后一行代码即可,如:

    popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]-popupWindow.getWidth(), location[1]); 
    
    

    ④.在控件的右边,只需修改最后一行代码即可,如:

    popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]+v.getWidth(), location[1]);
    

    最后的使用,你只需要在布局文件中设置一个可点击控件,然后在你需要的类中,把该控件findViewById()出来,然后给其设置setOnClickListener(…)方法即可,在进行事件的处理时调用showPopUp()方法即可。

    相关文章

      网友评论

          本文标题:[转]android PopupWindow在控件的各个方向上的

          本文链接:https://www.haomeiwen.com/subject/vvyrtctx.html