如图要实现点击+号时,出现popwindow:
//anchor锚点view
private void showSelectDialog(View anchor) {
View outerView = LayoutInflater.from(this).inflate(R.layout.dialog_chat, null);
//1.这里使用WRAP_CONTENT时可以根据布局大小来灵活掌握
PopupWindow popupWindow = new PopupWindow(outerView,ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setOnDismissListener(() -> {
Utils.changeWindowAlpha(1f, this);
});
Utils.changeWindowAlpha(0.7f, this);
popupWindow.showAsDropDown(anchor, 0, 0, Gravity.END);
}
/**
* 改变当前window的透明度
* @param alpha
* @param context
*/
public static void changeWindowAlpha(float alpha, Context context) {
WindowManager.LayoutParams params = ((Activity) context).getWindow().getAttributes();
params.alpha = alpha;
((Activity) context).getWindow().setAttributes(params);
// 此方法用来设置浮动层,防止部分手机变暗无效
//((Activity) mContext).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}
布局文件:R.layout.dialog_chat
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:layout_marginEnd="@dimen/dp_20"
android:background="@drawable/bg_chat_main"
android:orientation="vertical">
注意代码中1,2,3,4的注释,助你灵活掌握popwindow的大小和间距。
如果需要改成自定义PopupWindow形式如下:
public class CardFilterPop extends PopupWindow {
private Context mContext;
private View outerView;
private TextView tv_my_card,tv_scan;
private View anchor;//描点view
private PopWindowOnClickListener mOnClickListener;
public interface PopWindowOnClickListener {
void onResetClick();
void onSureClick();
}
public CardFilterPop(Context context,View anchor, List data,PopWindowOnClickListener listener){
mContext=context;
this.anchor=anchor;
mOnClickListener=listener;
setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
setFocusable(true);
setOutsideTouchable(true);
setOnDismissListener(() -> changeWindowAlpha(1f, mContext));
outerView = LayoutInflater.from(context).inflate(R.layout.dialog_chat, null);
initViews(data);
setContentView(outerView);
}
private void initViews(List data){
tv_my_card=outerView.findViewById(tv_my_card);
tv_scan=outerView.findViewById(R.id.tv_scan);
//省略一些业务代码
tv_my_card.setOnClickListener(view -> {
if(mOnClickListener!=null){
mOnClickListener.onResetClick();
}
});
tv_scan.setOnClickListener(view -> {
if(mOnClickListener!=null){
mOnClickListener.onSureClick();
}
});
}
public void showPop(){
//changeWindowAlpha(0.7f, mContext);
showAsDropDown(anchor, 0, 0, Gravity.END);
}
/**
* 改变当前window的透明度
* @param alpha
* @param context
*/
private void changeWindowAlpha(float alpha, Context context) {
WindowManager.LayoutParams params = ((Activity) context).getWindow().getAttributes();
params.alpha = alpha;
((Activity) context).getWindow().setAttributes(params);
}
}
调用时
private CardFilterPop cardFilterPop;
if (cardFilterPop == null) {
cardFilterPop=new CardFilterPop(getContext(), ll_filter, data, new CardFilterPop.PopWindowOnClickListener() {
@Override
public void onResetClick() {
}
@Override
public void onSureClick() {
}
});
}
cardFilterPop.showPop();