【问题标题】:Using WRAP_CONTENT, popupwindow gets hole screen width使用 WRAP_CONTENT,popupwindow 获取孔屏幕宽度
【发布时间】:2014-11-25 18:18:45
【问题描述】:

我正在尝试使用自定义布局创建 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
        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>

我在这里创建PopupWindow

PopupWindow popupWindow = new PopupWindow(view,
        LayoutParams.WRAP_CONTENT, 48);

但是它会获得孔屏幕宽度。我还检查了thisthis,但它们没有帮助。我该怎么办?

编辑:看来问题出在ImageView。在图像中使用wrap_content 也不起作用。所以我删除了ImageView 并在TextView 中使用了drawableRight 属性,它可以工作。

【问题讨论】:

  • 我认为它会因为您的图像中的 ImageView 正在显示。
  • 尝试修复TextView的宽度并检查。
  • @PiyushGupta 那么问题是什么?图片仅占 32*32 平方空间。
  • @HareshChhelana 问题是我不想使用固定宽度值。
  • @MisaghEmamverdi 找到解决方案了吗?

标签: android android-layout android-popupwindow


【解决方案1】:

您必须以编程方式创建弹出窗口。您不能在 XML 文件中更改它。我遇到了同样的问题。将 match_parent 放入您的 xml 中,而不是 wrap_content。做这样的事情:

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

希望对您有所帮助:)

【讨论】:

  • 正如我所说,我不希望我的弹出窗口具有固定宽度。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-02
  • 1970-01-01
  • 1970-01-01
  • 2021-10-30
  • 2010-11-25
  • 2011-09-25
相关资源
最近更新 更多