LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.message_popup, null);
TextView textView = view.findViewById(R.id.toast_msg);
textView.setText("最大收款金额:" + DataUtil.fenToYuanDivided(topAmount));
pop = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
pop.showAtLocation(targetView, Gravity.NO_GRAVITY, rect.left, rect.top - target.getHeight());
<?xml version="1.0" encoding="utf-8"?>
<layout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/toast_bg"
android:gravity="center">
<TextView
android:layout_marginHorizontal="@dimen/x24"
android:layout_marginVertical="@dimen/y16"
android:id="@+id/toast_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="28sp" />
</RelativeLayout>
</layout>
pop = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,true);
如果popupwindow的focusable为true时,poppup会消费点击事件,同时popup消失
布局时最好避免用padding,同时可以在外面再包一层布局来避免外层布局失效