關(guān)于PopupWindow的使用俄周,網(wǎng)上各種介紹,不勝枚舉髓迎。我這里記錄一下自己寫的一個小demo(備忘)峦朗,背景是半透明的,從底部彈起的popupwindow框排龄,然后封裝成通用父類波势。效果如下:
下面是通用父類的代碼(BasePopupWindow.java):
protected Activity activity;
protected View mContentView;
protected OnItemClickListener listener;
public BasePopupWindow(Activity activity) {
this.activity = activity;
initView();
}
/**
* 初始化控件
*/
private void initView() {
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mContentView = inflater.inflate(getLayout(), null);
bindView();
dealEvent();
//設(shè)置PopupWindow的View
this.setContentView(mContentView);
//設(shè)置PopupWindow彈出窗體的寬
this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
//設(shè)置PopupWindow彈出窗體的高
this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
//設(shè)置PopupWindow彈出窗體可點擊
this.setFocusable(true);
//設(shè)置PopupWindow彈出窗體動畫效果
this.setAnimationStyle(R.style.bottom_anim);
//關(guān)閉事件
this.setOnDismissListener(new popupDismissLister());
backgroundAlpha(0.5f);
//mMenuView添加OnTouchListener監(jiān)聽判斷獲取觸屏位置如果在選擇框外面則銷毀彈出框
mContentView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int height = mContentView.findViewById(getPopupTopView()).getTop();
int y = (int) event.getY();
if (event.getAction() == MotionEvent.ACTION_UP) {
if (y < height) {
dismiss();
}
}
return true;
}
});
}
/**
* 獲取布局文件
*
* @return
*/
protected abstract int getLayout();
/**
* 獲取PopupWindow最高控件
*
* @return
*/
protected abstract int getPopupTopView();
/**
* 綁定控件
*/
protected abstract void bindView();
/**
* 處理相關(guān)事件
*/
protected abstract void dealEvent();
/**
* 查找View
*
* @param id 控件的id
* @param <VT> View類型
* @return
*/
protected <VT extends View> VT getViewById(@IdRes int id) {
return (VT) mContentView.findViewById(id);
}
/**
* 設(shè)置popupwindow外面背景透明度
*
* @param bgalpha 透明度 0-1 0-透明 1-不透明
*/
private void backgroundAlpha(float bgalpha) {
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
lp.alpha = bgalpha;
activity.getWindow().setAttributes(lp);
}
/**
* 添加子空間的監(jiān)聽事件
*
* @param listener
*/
public void addOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
public interface OnItemClickListener {
void onClicked(int flag);
}
private class popupDismissLister implements PopupWindow.OnDismissListener {
@Override
public void onDismiss() {
backgroundAlpha(1f);
}
}
使用時,只需要繼承這個通用類接口橄维,以下便是我的小例子尺铣。
RemindPopWindow.java
public final static int FLAG_EDIT = 0;
public final static int FLAG_DELETE = 1;
TextView mEdit, mDelete, mCancel;
public RemindPopWindow(Activity activity) {
super(activity);
}
@Override
protected int getLayout() {
return R.layout.window_remind;
}
@Override
protected int getPopupTopView() {
return R.id.layout;
}
@Override
protected void bindView() {
mEdit = getViewById(R.id.tv_edit);
mDelete = getViewById(R.id.tv_delete);
mCancel = getViewById(R.id.tv_cancel);
}
@Override
protected void dealEvent() {
mEdit.setOnClickListener(this);
mDelete.setOnClickListener(this);
mCancel.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view == mEdit) {
if (listener!=null)listener.onClicked(FLAG_EDIT);
}
if (view == mDelete) {
if (listener!=null)listener.onClicked(FLAG_DELETE);
}
if (view == mCancel) {
dismiss();
}
}
提示框的布局window_remind.xml,ViewGroup為LinearLayout 挣郭,id為layout迄埃。
<TextView
android:id="@+id/tv_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:gravity="center"
android:minHeight="60dp"
android:text="編輯"
android:textColor="#5f5f5f"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1px"
android:background="@android:color/white"
android:gravity="center"
android:minHeight="60dp"
android:text="刪除"
android:textColor="#5f5f5f"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:background="@android:color/white"
android:gravity="center"
android:minHeight="60dp"
android:text="取消"
android:textColor="#5f5f5f"
android:textSize="16sp" />
這樣一來,提示的對話框就寫好了兑障,使用時侄非,只要在對應(yīng)的布局中顯示PopupWindow就行了。
具體代碼流译,請見:https://github.com/Henry-Mark/PopWindowDemo逞怨。
初次寫,如有啥問題福澡,還請大神們指正叠赦,謝謝。