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

标签 android popupwindow android-popupwindow

要创建一个简单的工作 PopupWindow,我们需要执行以下操作:

popup_example.xml:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:padding="10dip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <TextView         
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dip"
            android:text="Test Pop-Up" />

    </LinearLayout>

Java代码

LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup_example, null, false),100,100, true);

pw.showAtLocation(this.findViewById(R.id.main), Gravity.CENTER, 0, 0);

我的要求是我需要一个

<TEXTVIEW android:layout_height="wrap_content" android:layout_width="fill_parent" />

和一个

<BUTTON android:id="@+id/end_data_send_button" android:text="Cancel"/>

在我的popup_example.xml中。我如何在 Java 代码中处理这两个组件?

screenshot

最佳答案

如何制作一个简单的Android弹窗

这是一个更完整的示例。这是一个补充答案,一般涉及创建弹出窗口,而不一定涉及OP问题的具体细节。 (OP 要求取消按钮,但这不是必需的,因为用户可以单击屏幕上的任意位置来取消它。)它将如下图所示。

enter image description here

为弹出窗口进行布局

将布局文件添加到 res/layout 中,定义弹出窗口的外观。

popup_window.xml

<?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="wrap_content"
    android:background="#62def8">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="30dp"
        android:textSize="22sp"
        android:text="This is a popup window."/>

</RelativeLayout>

膨胀并显示弹出窗口

这是我们示例的主要 Activity 的代码。每当单击该按钮时,弹出窗口都会膨胀并显示在 Activity 上方。触摸屏幕上的任意位置都会关闭弹出窗口。

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onButtonShowPopupWindowClick(View view) {

        // inflate the layout of the popup window
        LayoutInflater inflater = (LayoutInflater)
                getSystemService(LAYOUT_INFLATER_SERVICE);
        View popupView = inflater.inflate(R.layout.popup_window, null);

        // create the popup window
        int width = LinearLayout.LayoutParams.WRAP_CONTENT;
        int height = LinearLayout.LayoutParams.WRAP_CONTENT;
        boolean focusable = true; // lets taps outside the popup also dismiss it
        final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);

        // show the popup window
        // which view you pass in doesn't matter, it is only used for the window tolken
        popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);

        // dismiss the popup window when touched
        popupView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                popupWindow.dismiss();
                return true;
            }
        });
    }
}

就是这样。你已经完成了。

继续

查看how gravity values effect PopupWindow .

PopupWindow bottom gravity with offsets

您还可以add a shadow .

PopupWindow with shadow

进一步研究

这些对于学习如何制作弹出窗口也很有帮助:

关于android - Android中如何创建弹出窗口(PopupWindow),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56040810/

相关文章:

android - 在两 Pane 布局中使用 SupportMapFrament 会导致重复布局错误

java - 试图在线程中按下PopupWindow的按钮

Android:无法选中PopupWindow中的ListView

java - Android PopupWindow 和 SeekBar 监听器没有响应

android - 通过后退按钮关闭弹出窗口

java - 无法解析类型 org.xmlpull.v1.XmlPullParser。它是从所需的 .class 文件中间接引用的

android - 为什么这是 9-Patching 的坏 PNG?

android - 检查设备上是否有可移动的 SD 卡

javascript - 如何同时打开多个javascript弹出窗口?

android - PopupWindow 的灰色背景不包括工具栏/操作栏