popwindow显示位置

先看效果图,免得浪费大家时间,看是不是想要的效果 。



直接上代码 ,核心方法。

[java]  view plain copy
  1. private void showPopupWindow(View parent) {  
  2.         if (popupWindow == null) {  
  3.             LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  4.             view = layoutInflater.inflate(R.layout.group_list, null);  
  5.             lv_group = (ListView) view.findViewById(R.id.lvGroup);  
  6.               
  7.             Collections.reverse(groups);  
  8.             GroupAdapter groupAdapter = new GroupAdapter(this, groups);  
  9.             lv_group.setAdapter(groupAdapter);  
  10.             popupWindow = new PopupWindow(view, 200220);  
  11.         }  
  12.         popupWindow.setFocusable(true);  
  13.         popupWindow.setOutsideTouchable(true);  
  14.         // 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景  
  15.         popupWindow.setBackgroundDrawable(new BitmapDrawable());  
  16.         WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);  
  17.         int xPos = -popupWindow.getWidth() / 2  
  18.                 + getCustomTitle().getCenter().getWidth() / 2;  
  19.   
  20.         popupWindow.showAsDropDown(parent, xPos, 4);  
  21.   
  22.         lv_group.setOnItemClickListener(new OnItemClickListener() {  
  23.             @Override  
  24.             public void onItemClick(AdapterView<?> adapterView, View view,  
  25.                     int position, long id) {  
  26.                 loadNew(((StringItem)(groups.get(position))).getId());  
  27.                 if (popupWindow != null)   
  28.                     popupWindow.dismiss();  
  29.             }  
  30.         });  
  31.     }  


这篇是转载的 。

popupWindow 在控件的各个方向上的显示(上、下、左、右),主要用到popupWindow 的showAtLocation()方法:

在控件的上方:

[java]  view plain copy
  1. private void showPopUp(View v) {  
  2.         LinearLayout layout = new LinearLayout(this);  
  3.         layout.setBackgroundColor(Color.GRAY);  
  4.         TextView tv = new TextView(this);  
  5.         tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
  6.         tv.setText("I'm a pop -----------------------------!");  
  7.         tv.setTextColor(Color.WHITE);  
  8.         layout.addView(tv);  
  9.   
  10.         popupWindow = new PopupWindow(layout,120,120);  
  11.           
  12.         popupWindow.setFocusable(true);  
  13.         popupWindow.setOutsideTouchable(true);  
  14.         popupWindow.setBackgroundDrawable(new BitmapDrawable());  
  15.           
  16.         int[] location = new int[2];  
  17.         v.getLocationOnScreen(location);  
  18.           
  19.         popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1]-popupWindow.getHeight());  
  20.     }  

在控件的其他方向上显示只需修改最后一行代码即可,如:

下方:popupWindow.showAsDropDown(v);

左边:

[java]  view plain copy
  1. popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]-popupWindow.getWidth(), location[1]);  
右边:
[html]  view plain copy
  1. popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]+v.getWidth(), location[1]);  
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值