弹出窗口(使用PopupWindow或Dialog)无明显原因会撑满屏幕宽度

7

我希望你能协助解决弹出框的问题!=)

问题是弹出框“填充”了整个屏幕的宽度,即使布局清楚地表明应该使用“wrap_content”也没有用。无论是使用Dialog还是PopupWindow都不行。

首先,这是弹出框popup_message.xml的XML布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    android:background="@android:color/transparent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal"
        android:layout_gravity="center" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:background="@android:color/transparent"
            android:gravity="center_vertical|center_horizontal"
            android:orientation="horizontal"
            android:paddingLeft="10dp"
            android:text="@string/new_message"
            android:textColor="#000000"
            android:textAllCaps="true" >

        </TextView>
    </LinearLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="15dp"
        android:background="#ffffff"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/popup_message_textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textCursorDrawable="@null" >
        </TextView>

        <Button
            android:id="@+id/jobview_pickup_start_submit"
            android:layout_width="fill_parent"
            android:layout_height="35dp"
            android:layout_below="@+id/popup_message_textView"
            android:layout_gravity="center_vertical|center_horizontal"
            android:layout_marginBottom="15dp"
            android:layout_marginTop="15dp"
            android:gravity="center_horizontal"
            android:text="Confirm"
            android:textColor="#000000" />
    </RelativeLayout>
</LinearLayout>

我使用的代码:

LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View input = inflater.inflate(R.layout.popup_message, null);
PopupWindow pw = new PopupWindow(input, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
View v = findViewById(R.id.main_deviceInfoBar);
pw.showAtLocation(v, Gravity.CENTER, 0, 0);

我还尝试过以下代码:

Dialog dialogManualStart = new Dialog(MainActivity.this); dialogManualStart.getWindow().setBackgroundDrawableResource(R.color.transparent); dialogManualStart.requestWindowFeature(MainActivity.this.getWindow().FEATURE_NO_TITLE); dialogManualStart.setContentView(input); dialogManualStart.show();

无论我使用什么代码,效果都是这样的:

enter image description here

可以看到,它总是填满屏幕宽度。

问题:有人能告诉我为什么吗?

======== 编辑 1 =============

我将按钮设置为“wrap_content”(根据Boogers的建议),然后它看起来像这样:

enter image description here

这是非常奇怪的行为,不是我期望的。

如果我将其更改为“match_parent”,它就会回到“全宽”,即从左到右延伸。


这是因为你的TextView设置了match_parent。 - njzk2
你是什么意思?因为我试过改变那个,但没有用 - 仍然是全宽... - Ted
match_parent 表示占用所有可用空间。wrap_content 表示尽可能少地占用空间。在 wrap_content 中使用 match_parent 表示父元素会尽可能宽以适应子元素。 - njzk2
好的,就像我说的...如果我更改那个值,什么都不会改变。而“match_parent”意味着“占用可用空间”,但它不应该是全宽度,因为顶级布局是“wrap_content”,那么为什么它会扩展? - Ted
没有理由使用wrap_content作为match_parent来缩小子视图。更有意义的是扩展父视图。 - njzk2
1个回答

0

这是你的问题:

<Button
            android:id="@+id/jobview_pickup_start_submit"
            android:layout_width="fill_parent"
...

将其从“fill_parent”更改为“wrap_content”或“match_parent”


这个问题是怎么回事呢?它说“fill_parent”,而它的父级是:1)RelativeLayout和2)顶部LinearLayout。所以当它填充其父级时,它填充了RelativeLayout,但那不是“fill_width”...我的意思是这里没有任何东西可以“扩展”宽度...假设我想让按钮填充宽度,无论它是什么。但是我已经通过其他布局限制了它,我该怎么办? - Ted
请看我上面的编辑1。你们两个的建议似乎都不是我想要的。 - Ted
我的建议实际上对你起作用了(并且是正确的答案)。它按预期包装内容。我回答了你最初的问题(现在你有一个不同的问题 - 但是你的对话框不是全宽,这就是你当时问的)。 - Booger
1
好的,那是真的。但行为仍然完全不合逻辑。 - Ted

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接