Android 弹出窗口不填满屏幕大小?

标签 android popupwindow android-popupwindow

我正在尝试制作一个简单的弹出窗口。但每次我做一个,它最终都会变得非常小……而不是我想要的长度。这是弹出窗口的样子: enter image description here

这是我的弹出窗口布局:

<?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();
            }
        };

无法弄清楚为什么它不起作用... 我怎样才能得到我想要的尺寸?

最佳答案

在这里您不能使用弹出窗口 xml 中的布局。您必须使用主布局中的任何 View 。现在我正在使用 FloatingButton 作为 showAtLocation 的 View 。

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();
            }
        }

关于Android 弹出窗口不填满屏幕大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38987442/

相关文章:

java - java的console.log()是什么?

java - WebView - 使用凭据的正确方法

android - 弹出窗口:ShowAsDropDown 显示为右上角而不是左下角?

javascript - 弹出窗口未在 codeigniter 中打开

android - 使用广播 Intent/广播接收器将消息从服​​务发送到 Activity

android - 如何在java canvas中启用android抗锯齿功能?

javascript - 在我第一次访问该页面时单击 .popup-close 后,如何禁止弹出窗口在给定页面上显示 2 天?

android - 显示 PopupWindow 时未调用 onBackPressed

java - 使用 Runnable Thread Android 创建弹出窗口

android - Android中如何创建弹出窗口(PopupWindow)