Android 之popupWindow 在指定位置上的显示

(一)popupwindow位置的设定

在平时使用popupwindow的时候经常会为了显示位置而纠结,这篇文章主要介绍了popupWindow 在控件的各个方向上的显示,主要用到popupWindow 的showAtLocation()方法:

这个方法的四个参数的 含义:

* @param parent a parent view to get the {@link android.view.View#getWindowToken()} token from
* @param gravity the gravity which controls the placement of the popup window
* @param x the popup's x location offset
* @param y the popup's y location offset

下面是一些设置popupwindow显示位置的例子,如在控件的上方:

[java] view plaincopy
  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.setTextColor(Color.WHITE);  
  7.         layout.addView(tv);  
  8.   
  9.         popupWindow = new PopupWindow(layout,120,120);  
  10.           
  11.         popupWindow.setFocusable(true);  
  12.         popupWindow.setOutsideTouchable(true);  
  13.         popupWindow.setBackgroundDrawable(new BitmapDrawable());  
  14.           
  15.         int[] location = new int[2];  
  16.         v.getLocationOnScreen(location);  
  17.           
  18.         popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1]-popupWindow.getHeight());  
  19.     }  

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

下方:popupWindow.showAsDropDown(v);

当然也可以使用showAsLocation方法,设置x,y参数时设定;

左边:

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

右边:

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


(2)设定popupwindow点击屏幕其他位置消失

网上大多的说法是这样设定的:

<span style="font-size:14px;">popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);</span>

这样设定之后我们实验一下还是不管用的,找了半天原因,原来是要想使popupwindow点击屏幕其他地方消失,需要为popupwindow设定背景,但是我们开发的时候可能已经在popupwindow设定了背景,怎么办呢?


<span style="font-size:14px;">/**
     * Specifies the background drawable for this popup window. The background
     * can be set to {@code null}.
     *
     * @param background the popup's background
     * @see #getBackground()
     * @attr ref android.R.styleable#PopupWindow_popupBackground
     */
    public void setBackgroundDrawable(Drawable background) {
        mBackground = background;

        // If this is a StateListDrawable, try to find and store the drawable to be
        // used when the drop-down is placed above its anchor view, and the one to be
        // used when the drop-down is placed below its anchor view. We extract
        // the drawables ourselves to work around a problem with using refreshDrawableState
        // that it will take into account the padding of all drawables specified in a
        // StateListDrawable, thus adding superfluous padding to drop-down views.
        //
        // We assume a StateListDrawable will have a drawable for ABOVE_ANCHOR_STATE_SET and
        // at least one other drawable, intended for the 'below-anchor state'.
        if (mBackground instanceof StateListDrawable) {
            StateListDrawable stateList = (StateListDrawable) mBackground;

            // Find the above-anchor view - this one's easy, it should be labeled as such.
            int aboveAnchorStateIndex = stateList.getStateDrawableIndex(ABOVE_ANCHOR_STATE_SET);

            // Now, for the below-anchor view, look for any other drawable specified in the
            // StateListDrawable which is not for the above-anchor state and use that.
            int count = stateList.getStateCount();
            int belowAnchorStateIndex = -1;
            for (int i = 0; i < count; i++) {
                if (i != aboveAnchorStateIndex) {
                    belowAnchorStateIndex = i;
                    break;
                }
            }

            // Store the drawables we found, if we found them. Otherwise, set them both
            // to null so that we'll just use refreshDrawableState.
            if (aboveAnchorStateIndex != -1 && belowAnchorStateIndex != -1) {
                mAboveAnchorBackgroundDrawable = stateList.getStateDrawable(aboveAnchorStateIndex);
                mBelowAnchorBackgroundDrawable = stateList.getStateDrawable(belowAnchorStateIndex);
            } else {
                mBelowAnchorBackgroundDrawable = null;
                mAboveAnchorBackgroundDrawable = null;
            }
        }
    }</span>

看了一下popupwindow setBackgriyndDrawable的源代码,里面除了设定mBackground之外还有一些其他的业务逻辑,好了,看来只能这样设置了,但是为了不影响我们已经在popupwindow view中设定的背景,这里我们直接设定一个透明背景好了:

<span style="font-size:14px;">popupWindow.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.menu_bg));</span>

再看一下menu_bg资源文件的源代码:

<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid
        android:color="@android:color/transparent"/><!-- 填充的颜色 -->

</shape></span>

很简单就是一个透明的背景,这样再次点击popupwindow之外的屏幕,popupwindow就可以消失了。。。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值