在Android设计弹出框是一个很常见的交互方式。弹出框的具体实现方式主要有两种一种是Dialog,另一种是PopupWindow。这两者的主要区别是Dialog的显示的位置是固定,而PopupWindow的位置是任意的。关于PopupWindow的官方描述如下:
This class represents a popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current activity.
文档描述:PopupWindow可以显示任意的View,而且PopupWindow以悬浮在Activity的上部的任意位置。
PopupWindow的使用
PopupWindow的使用比较简单,主要有2步
- 构建PopupWindow对象并进行必要的初始设置。
- 调用PopupWindow的显示方法,显示PopupWindow对象。
PopupWindow的构建方法有很多,方法如下
| 方法 | 方法描述 |
|---|---|
| PopupWindow(Context context) | 创建一个空的,不可以focusable,尺寸为(0,0)的popupwindow |
| PopupWindow(Context context, AttributeSet attrs) | 同上 |
| PopupWindow(Context context, AttributeSet attrs, int defStyleAttr) | 同上 |
| PopupWindow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) | 同上 |
| PopupWindow() | 同上 |
| PopupWindow(View contentView) | 创建一个显示View为contentview,不可focusable的popupwindow,尺寸为(0,0) |
| PopupWindow(int width, int height) | 创建一个空的,不可focusable的popupwindow |
| PopupWindow(View contentView, int width, int height) | 创建一个显示View为contentview,不可focusable的popupwindow,尺寸为(width,height) |
| PopupWindow(View contentView, int width, int height, boolean focusable) | 创建一个显示View为contentview,focusable状态为popupwindow,尺寸为(width,height) |
具体使用那个构造方法可以根据具体的场景使用。
PopupWindow的一般的初始化的代码如下:
View contentView=LayoutInflater.from(context).inflate(R.layout.popup_window_content,null,false);
PopupWindow popupWindow=new PopupWindow(context);
popupWindow.setContentView(contentView);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
注意:对于PopupWindow的初始化必须设置3个参数:setContentView,setHeight,setWidth。否则PopupWindow存在无法显示的风险。
PopupWindow的显示主要有4个方法。
| 方法 | 方法描述 |
|---|---|
| showAsDropDown(View anchor) | 在anchor的左下方显示PopupWindow |
| showAsDropDown(View anchor, int xoff, int yoff, int gravity) | 同上 |
| showAsDropDown(View anchor, int xoff, int yoff) | 同上 |
| showAtLocation(View parent, int gravity, int x, int y) | 在parent的指定的位置显示popupWindow |
PopupWindow的显示代码可以如下代码显示:
popupWindow.showAsDropDown(anchorView);
一个简单的的PopupWindow显示的通用类
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
public class PopupWindowHelper {
private PopupWindow mPopupWindow;
private View mContentView;
private int mWidth;
private int mHeight;
private boolean mOutsideTouchable;
private boolean mFocusable;
private Context mContext;
public PopupWindowHelper(Context context) {
mContext = context;
}
private void build() {
if (mWidth != 0 && mHeight != 0) {
mPopupWindow = new PopupWindow(mWidth, mHeight);
} else {
mPopupWindow = new PopupWindow(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
if (mContentView != null) {
mPopupWindow.setContentView(mContentView);
}
mPopupWindow.setHeight(mHeight);
mPopupWindow.setWidth(mWidth);
mPopupWindow.setFocusable(mFocusable);
mPopupWindow.setTouchable(mOutsideTouchable);
}
public void showAsDropDown(View anchor) {
if (mPopupWindow != null) {
mPopupWindow.showAsDropDown(anchor);
}
}
public void dismiss(){
if(mPopupWindow!=null){
mPopupWindow.dismiss();
}
}
public static class PopupWindowBuilder {
private PopupWindowHelper popupWindowHelper;
private Context mContext;
public PopupWindowBuilder(Context context) {
mContext = context;
popupWindowHelper = new PopupWindowHelper(mContext);
}
public PopupWindowBuilder setContentView(int resId) {
popupWindowHelper.mContentView = View.inflate(mContext, resId, null);
return this;
}
public PopupWindowBuilder setContentView(View contentView) {
popupWindowHelper.mContentView = contentView;
return this;
}
public PopupWindowBuilder setSize(int width, int height) {
popupWindowHelper.mWidth = width;
popupWindowHelper.mHeight = height;
return this;
}
public PopupWindowBuilder setOutsideTouchable(boolean touchable) {
popupWindowHelper.mOutsideTouchable = touchable;
return this;
}
public PopupWindowBuilder setFocusable(boolean focusable) {
popupWindowHelper.mFocusable = focusable;
return this;
}
public PopupWindowHelper create() {
popupWindowHelper.build();
return popupWindowHelper;
}
}
}
使用实例
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private PopupWindowHelper mPopupWindowHelper;
private View contentView;
private Button btnView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnView = (Button) findViewById(R.id.btn_settopic);
btnView.setOnClickListener(this);
contentView = LayoutInflater.from(this).inflate(R.layout.popup_content, null, false);
contentView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPopupWindowHelper.dismiss();
}
});
mPopupWindowHelper = new PopupWindowHelper.PopupWindowBuilder(this)
.setContentView(contentView)
.setSize(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
.setFocusable(true)
.setOutsideTouchable(true)
.create();
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_settopic) {
mPopupWindowHelper.showAsDropDown(btnView);
}
}
}