是否有使用 Android v2.0 的 PopupWindow 类的简单示例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1967863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:59:03  来源:igfitidea点击:

Is there a simple example of the PopupWindow class using Android v2.0?

androidandroid-popupwindow

提问by Todd

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 类的工作示例。我在网上找到的代码示例要么编译但不起作用,要么正在使用已被删除的方法(例如Activity.getViewInflate()).

Is there a simple workingexample that displays a PopupWindow?

是否有一个显示 PopupWindow的简单工作示例?

回答by Todd

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

我根据这篇 Google Groups 帖子创建了一个工作示例

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

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

  1. Create a layout XML which describes the View that will be rendered within the PopupWindow.
  2. Invoke the PopupWindow by inflating the layout XML, and assign the appropriate "parent view" to the pop-up.
  1. 创建一个布局 XML,它描述将在 PopupWindow 中呈现的视图。
  2. 通过扩充布局 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:

爪哇代码:

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

回答by Ravindranath Akila

AFAIK only the AbsoluteLayout works(pls confirm), as seen on http://sree.cc/google/android/android-popup-window. I've shown the popup right, but LinearLayout was not showing all elements. But AbsoluteLayout is deprecated!

AFAIK 只有AbsoluteLayout 有效(请确认),如http://sree.cc/google/android/android-popup-window 所示。我已经正确显示了弹出窗口,但是 LinearLayout 没有显示所有元素。但是 AbsoluteLayout 已被弃用!

FrameLayout also works, but organizing views is a nightmare since the official documentation says it is only good for holding one view.

FrameLayout 也有效,但组织视图是一场噩梦,因为官方文档说它只适用于持有一个视图。

Also, to be able to receive touch events, you need to do this: setBackgroundDrawable(new BitmapDrawable());

此外,为了能够接收触摸事件,您需要这样做: setBackgroundDrawable(new BitmapDrawable());

as further explained at Android popup window dismissal

正如在Android 弹出窗口关闭中进一步解释的那样

回答by raman

You are getting the invisibility because you didn't set the Background color of the layout to whom you are inflated.set it as android:background="#778899",and definitely you can see the things

你得到了不可见性,因为你没有设置你被膨胀的布局的背景颜色。将它设置为 android:background="#778899",你肯定可以看到这些东西