PopupWindow 显示位置 大小的设置

一般是通过点击(图片、button等)事件触发的,显示一个PopWindow;


我们点击弹出

但是一般情况下popupWindpw会显示紧挨着被点击的按钮的下面,而不是如图所示的那样,这里就需要设置PopupWindow的显示位置了

private void showPopupWindow(View view){
//一个自定义的布局作为显示的内容
View contentView = LayoutInflater.from(ConvenienceStoreActivity.this).inflate(R.layout.imagebutton_pop_window, null);
WindowManager manger = (WindowManager) getSystemService(ConvenienceStoreActivity.WINDOW_SERVICE);
@SuppressWarnings("deprecation")
int width = manger.getDefaultDisplay().getWidth()/3;
PopupWindow popupWindow = new PopupWindow(contentView,width,300,true);

// 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框(必须设置)
        popupWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.pop_window_shape));
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        //距离被点击的控件下方15dp
popupWindow.showAsDropDown(view,0,15);//showAsDropDown(View anchor,int xoff ,int yoff);
click(contentView);


}
public void click(View view){//PopWindow的布局控件的点击事件的处理
switch (view.getId()) {
case R.id.about_bianlidian:
Toast.makeText(this, "关于便利店", Toast.LENGTH_SHORT).show();
break;


case R.id.nearby_bianlidian:
Toast.makeText(this, "附近便利店", Toast.LENGTH_SHORT).show();
break;
case R.id.share_bianlidian:
Toast.makeText(this, "分享便利店", Toast.LENGTH_SHORT).show();
showShare();
break;
}
}

此外,你还可以设置:

//获取在屏幕中的坐标
        int[] location = new int[2]; 
        v.getLocationOnScreen(location);


        popupWindow.setContentView(view);
    //   popupWindow.showAsDropDown(v,0, 10);

//设置相对于v的位置 或以自己设置
        popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1]+v.getHeight());

应用:

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

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

 

 

//居中显示

View v = mInflater.inflate(R.layout.givekala_share, null);
  Button share = (Button) v.findViewById(R.id.givekala_share);
  Button close = (Button)v.findViewById(R.id.givekala_share_close);

  Drawable drawable = getResources().getDrawable(R.drawable.givekala_share);
  PopupWindow popupWindow = new PopupWindow(context);
  popupWindow.setHeight(LayoutParams.WRAP_CONTENT);
  popupWindow.setWidth(LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(drawable);
        popupWindow.setOutsideTouchable(false);
        popupWindow.setFocusable(true);
  popupWindow.setContentView(v);
      //   popupWindow.showAsDropDown(v,0, 10);
  //设置相对于v的位置 或以自己设置
  popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);

参考网址:http://blog.sina.com.cn/s/blog_65f2f5c20102v38g.html
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值