android 父视图底部,Android PopupWindow 使用小Demo,以及从底部弹起的半透明背景的PopupWindow通用父类...

关于PopupWindow的使用,网上各种介绍,不胜枚举。我这里记录一下自己写的一个小demo(备忘),背景是半透明的,从底部弹起的popupwindow框,然后封装成通用父类。效果如下:

aaeda50eb2a5

I(img.png

下面是通用父类的代码(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();

//设置PopupWindow的View

this.setContentView(mContentView);

//设置PopupWindow弹出窗体的宽

this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);

//设置PopupWindow弹出窗体的高

this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

//设置PopupWindow弹出窗体可点击

this.setFocusable(true);

//设置PopupWindow弹出窗体动画效果

this.setAnimationStyle(R.style.bottom_anim);

//关闭事件

this.setOnDismissListener(new popupDismissLister());

backgroundAlpha(0.5f);

//mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框

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();

/**

* 处理相关事件

*/

protected abstract void dealEvent();

/**

* 查找View

*

* @param id 控件的id

* @param View类型

* @return

*/

protected VT getViewById(@IdRes int id) {

return (VT) mContentView.findViewById(id);

}

/**

* 设置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);

}

/**

* 添加子空间的监听事件

*

* @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。

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" />

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" />

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" />

这样一来,提示的对话框就写好了,使用时,只要在对应的布局中显示PopupWindow就行了。

初次写,如有啥问题,还请大神们指正,谢谢。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值