Android弹出窗口未填充屏幕大小?

16

我正在尝试制作一个简单的弹出窗口。但每次我制作一个,它最终都会非常小……而且不是我想要的长度。这就是弹出窗口的外观:

输入图片描述

这是我的弹出窗口布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popup_element"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#444444"
    android:padding="10px"
    android:orientation="vertical">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Transfering data"
        android:textColor="@color/white"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="Status"
        android:textColor="@color/white"/>

    <TextView android:id="@+id/server_status_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Awaiting answer..."
        android:paddingLeft="10sp"
        android:textColor="@color/white"/>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:gravity="center_horizontal|bottom">

        <Button android:id="@+id/end_data_send_button"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:drawablePadding="3sp"
            android:layout_centerHorizontal="true"
            android:text="Cancel" />
    </LinearLayout>
</LinearLayout>

这是我的Java代码:

private PopupWindow pw;
        private void bindActivity() {

            fabButton = (ImageButton) findViewById(R.id.activity_profileView_FAB);
            fabButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    initiatePopupWindow();
                }
            });


        }
     private void initiatePopupWindow() {
            try {
                //We need to get the instance of the LayoutInflater, use the context of this activity
                LayoutInflater inflater = (LayoutInflater) ProfileView.this
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                //Inflate the view from a predefined XML layout
                View layout = inflater.inflate(R.layout.popup,
                        (ViewGroup) findViewById(R.id.popup_element));
                // create a 300px width and 470px height PopupWindow
                pw = new PopupWindow(layout, 300, 470, true);
                // display the popup in the center
                pw.showAtLocation(layout, Gravity.CENTER, 0, 0);

                TextView mResultText = (TextView) layout.findViewById(R.id.server_status_text);
                Button cancelButton = (Button) layout.findViewById(R.id.end_data_send_button);
                cancelButton.setOnClickListener(cancel_button_click_listener);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        private View.OnClickListener cancel_button_click_listener = new View.OnClickListener() {
            public void onClick(View v) {
                pw.dismiss();
            }
        };

无法弄清楚为什么它不能工作... 我该如何获得我想要的大小?


这是一个示例弹出窗口。https://dev59.com/u2Yr5IYBdhLWcg3w29vi - Tai Nguyen
@TaiNguyen 但我不想使用Dialog Fragment,我想使用Popupwindow类。我只是想确保我能够使用Popupwindow类创建它。 - TheQ
这是一个使用PopupWindow的好样例:http://mrbool.com/how-to-implement-popup-window-in-android/28285。在showAtLocation之前,您应该设置setContentView。popupMessage.setContentView(layoutOfPopup); - Tai Nguyen
2
// 创建一个宽度为300像素,高度为470像素的PopupWindow。我看不出有什么问题,你得到了你要求的东西;-) - Andrew Sun
请查看如何制作简单的Android弹出窗口。(这个答案曾经在这里,但我将其移动到了另一个问题中,因为它更适合那个问题。) - Suragch
2个回答

14

在弹出窗口 xml 中,您不能使用布局,您必须使用主布局中的任何视图。现在我正在使用 FloatingButton 作为视图以 showAtLocation 显示。

fabButton = (ImageButton) findViewById(R.id.activity_profileView_FAB);
            fabButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {
                    initiatePopupWindow(v);
                }
            });

 private void initiatePopupWindow(View v) {
            try {
                //We need to get the instance of the LayoutInflater, use the context of this activity
                LayoutInflater inflater = (LayoutInflater) ProfileView.this
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                //Inflate the view from a predefined XML layout
                View layout = inflater.inflate(R.layout.popup,
                        (ViewGroup) findViewById(R.id.popup_element));
                // create a 300px width and 470px height PopupWindow
                pw = new PopupWindow(layout, 300, 470, true);
                // display the popup in the center
                pw.showAtLocation(v, Gravity.CENTER, 0, 0);

                TextView mResultText = (TextView) layout.findViewById(R.id.server_status_text);
                Button cancelButton = (Button) layout.findViewById(R.id.end_data_send_button);
                cancelButton.setOnClickListener(cancel_button_click_listener);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

5

只需要更改

 pw = new PopupWindow(layout, 300, 470, true);

要喜欢这样的

pw = new PopupWindow(layout, LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT, true);

如果您需要在任何一侧添加边距,请在弹出的XML文件中进行设置。同时,在XML中使用android:layout_height="wrap_content"。它的优点是,如果您设置了边距,它将在任何设备屏幕上看起来相同。

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