如图要实现点击+号时,出现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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
<!--2.在最外层LinearLayout设置精确的宽高值是无效的,所以里面需要再包裹一层-->
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!--3.此处的宽高才是真正在屏幕上显示的大小,具体为图中红点的2和3-->
<LinearLayout
android:layout_width="@dimen/dp_120"
android:layout_height="@dimen/dp_120"
android:paddingStart="@dimen/dp_10"
<!--4.此处的marginEnd才是真正距离屏幕右侧的大小,具体为图中红点的1-->
android:layout_marginEnd="@dimen/dp_20"
android:background="@drawable/bg_chat_main"
android:orientation="vertical">
<TextView
android:id="@+id/tv_my_card"
android:layout_width="wrap_content"
android:layout_marginTop="@dimen/dp_25"
android:textColor="@color/color_333333"
android:textSize="@dimen/sp_16"
android:drawablePadding="@dimen/dp_5"
android:drawableStart="@drawable/icon_my_card"
android:layout_height="wrap_content"
android:text="@string/wo_de_ming_pian"/>
<TextView
android:id="@+id/tv_scan"
android:layout_width="wrap_content"
android:layout_marginTop="@dimen/dp_25"
android:textColor="@color/color_333333"
android:textSize="@dimen/sp_16"
android:drawablePadding="@dimen/dp_5"
android:drawableStart="@drawable/icon_scan"
android:layout_height="wrap_content"
android:text="@string/sao_yi_sao"/>
</LinearLayout>
</LinearLayout>
注意代码中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<String> 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<String> 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();