【问题标题】:PopupWindow not inflating on API 19PopupWindow 在 API 19 上没有膨胀
【发布时间】:2020-01-13 07:47:59
【问题描述】:

我的 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>

【问题讨论】:

  • 您是否收到任何错误。在具有 API 28,29 的真实设备或具有 API 19 的模拟器上尝试。另外,这里的 mContext 是什么?是 getApplicationContext() 还是 ClassName.this?
  • classname.this 没有错误
  • 请在具有 API 28,29 的真实设备或具有 API 19 的模拟器上尝试。我将尝试重新运行代码。
  • @davids。请显示您的布局 xml
  • @davids。给RelativeLayout 一个精确的dpmatch_parent 试试

标签: android android-popupwindow


【解决方案1】:

很奇怪,但添加这个解决了我的问题...

popupWindow.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

https://stackoverflow.com/a/51177435/11110509

这在我的其他非 API 19 设备上不需要

【讨论】:

    【解决方案2】:

    请尝试

    popupWindow.showAtLocation(mRelativeLayout, Gravity.CENTER,0,0);
    

    这里的 mRelativeLayout 是来自您的活动/片段的视图参考

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 2021-08-30
    • 2020-04-04
    • 1970-01-01
    相关资源
    最近更新 更多