(一)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显示位置的例子,如在控件的上方:
- 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.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);
当然也可以使用showAsLocation方法,设置x,y参数时设定;
左边:
- popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]-popupWindow.getWidth(), location[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就可以消失了。。。