Android 使用WRAP_内容,popupwindow获取孔屏幕宽度

Android 使用WRAP_内容,popupwindow获取孔屏幕宽度,android,android-layout,android-popupwindow,Android,Android Layout,Android Popupwindow,我正在尝试创建一个自定义布局的弹出窗口。这是我的布局文件: <?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="48dp" > <ImageView

我正在尝试创建一个自定义布局的弹出窗口。这是我的布局文件:

<?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="48dp" >

    <ImageView
        android:id="@+id/switch_iamge"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="4dp"
        android:src="@drawable/switch_icon" />

    <TextView
        android:id="@+id/old_info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/switch_iamge" />

</RelativeLayout>
然而,它得到了孔屏幕宽度。我也检查了一下,但是他们没有帮助。我该怎么办


编辑:问题似乎在ImageView中。在图像中使用wrap_内容也不起作用。因此,我删除了ImageView并在TextView中使用了drawableRight属性,它可以正常工作。

您必须以编程方式创建弹出窗口。您不能在XML文件中更改它。我也面临同样的问题。将匹配父项放入xml中,而不是包装内容。这样做:

PopupWindow popupWindow = new PopupWindow(view,120,220);

希望它能有所帮助:

我想会有帮助,因为图像中的ImageView正在显示。请尝试修复TextView宽度并进行检查。@PiyushGupta那么问题出在哪里?图像只占用32*32平方空间。@Hareshchelana问题是我不想对宽度使用固定值。@Misaghamverdi找到了解决方案?正如我所说,我不想让弹出窗口具有固定宽度。
PopupWindow popupWindow = new PopupWindow(view,120,220);