有没有采用​​Android 2.0版的PopupWindow类的一个简单的例子?例子、简单、Android、PopupWindow

2023-09-11 12:36:05 作者:栖止你掌

我在网上看了,没能找到PopupWindow类的工作示例。在code例子我在网上找到任何编译,但不工作,或者正在使用它,至今已删除的方法(如 Activity.getViewInflate())

I looked online and was not able to find a working example of the PopupWindow class. The code examples I found online either compile but do not work, or are using methods which have since been removed (such as Activity.getViewInflate()).

有一个简单的工作的例子,显示PopupWindow?

Is there a simple working example that displays a PopupWindow?

推荐答案

我创建了一个基于工作示例this谷歌论坛帖子。

I created a working example based on this Google Groups post.

要创建一个简单的工作PopupWindow,你需要做到以下几点:

To create a simple working PopupWindow, you'll need to do the following:

创建一个布局的XML描述将要在PopupWindow中呈现的视图。 将充气布局XML调用PopupWindow,并指定相应的父视图,以弹出。

popup_example.xml:

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的code:

Java code:

    LayoutInflater inflater = (LayoutInflater)
       this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    PopupWindow pw = new PopupWindow(
       inflater.inflate(R.layout.popup_example, null, false), 
       100, 
       100, 
       true);
    // The code below assumes that the root container has an id called 'main'
    pw.showAtLocation(this.findViewById(R.id.main), Gravity.CENTER, 0, 0);