我的PopupWindow在我的API 28,29模拟器上运行得很好,但是不确定为什么我的API 19真实设备上没有显示任何视图。该窗口肯定是创建的,因为当我单击使视图变大的按钮时,它就被聚焦了。
我的自定义弹出窗口:
public class MessagesMoreMenu extends PopupWindow {
private static final String TAG = "MessagesMoreMenu";
private Context mContext;
public MessagesMoreMenu(Context context) {
super(context);
this.mContext = context;
setupView();
}
private void setupView(){
View view = LayoutInflater.from(mContext)
.inflate(R.layout.popupmenu_messagesmoremenu, null);
ButterKnife.bind(this, view);
setContentView(view);
}
}
用途:
private void inflateMoreMenu(View view){
PopupWindow popupWindow = new MessagesMoreMenu(mContext);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.showAsDropDown(view);
}
XML布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/popupmenu_messagesmore_report"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Report"
android:padding="16dp"
app:drawableLeftCompat="@drawable/icon_messages_report"
android:textColor="@color/colorBlackFont"
android:layout_alignParentTop="true"
android:gravity="center_vertical"
android:drawablePadding="16dp"/>
<TextView
android:id="@+id/popupmenu_messagesmore_block"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="16dp"
android:padding="16dp"
android:text="Block"
app:drawableLeftCompat="@drawable/icon_alertdialog_block"
android:textColor="@color/colorBlackFont"
android:layout_below="@+id/popupmenu_messagesmore_report"
android:gravity="center_vertical"/>
</RelativeLayout>
请尝试
popupWindow.showAtLocation(mRelativeLayout, Gravity.CENTER,0,0);
这里mRelativeLayout是您的活动/片段中的视图引用
很奇怪,但是添加了这个解决了我的问题...
popupWindow.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
https://stackoverflow.com/a/51177435/11110509
在我的其他非API 19设备上不需要此