【问题标题】:Is there a simple example of the PopupWindow class using Android v2.0?是否有使用 Android v2.0 的 PopupWindow 类的简单示例?
【发布时间】:2010-12-30 09:15:09
【问题描述】:

我在网上查找并找不到 PopupWindow 类的工作示例。我在网上找到的代码示例要么编译但不起作用,要么正在使用已被删除的方法(例如Activity.getViewInflate())

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

【问题讨论】:

    标签: android android-popupwindow


    【解决方案1】:

    我基于this Google Groups post.创建了一个工作示例

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

    1. 创建一个布局 XML,用于描述将在 PopupWindow 中呈现的视图。
    2. 通过扩展布局 XML 来调用 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);
        // The code below assumes that the root container has an id called 'main'
        pw.showAtLocation(this.findViewById(R.id.main), Gravity.CENTER, 0, 0); 
    

    【讨论】:

    • 当我尝试你的示例代码时,它似乎在工作,但它真的很奇怪。弹出窗口一定会出现,但它几乎就好像它几乎完全透明一样。我可以在一个主要的 GUI 按钮上看到我的 TextView 的幻影,但 EditText 和 Button 完全不可见。我查看了函数签名,找不到任何关于不透明度的信息。你能想到什么可能导致这种行为吗?
    • 抱歉,Dave,我有一段时间没有接触 Android,所以我对这段特定代码的记忆已经淡化。我发现这个关于不透明度/阿尔法的问题。希望对您有所帮助:stackoverflow.com/questions/2838757/…
    • 弹出窗口完全透明的原因在 PopupWindow 的 javadoc 中有回答:The popup does not provide any background. This should be handled by the content view.。因此,您的弹出视图布局根应指定一个 android:background="" 属性。
    • 你能解释一下“根容器”的含义吗?是弹出窗口开始的视图吗?
    • root container 这个词是什么意思?
    【解决方案2】:

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

    FrameLayout 也可以使用,但组织视图是一场噩梦,因为官方文档说它只对保持一个视图有用。

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

    Android popup window dismissal 进一步解释

    【讨论】:

      【解决方案3】:

      你得到了隐形,因为你没有设置你被充气的布局的背景颜色。将它设置为 android:background="#778899",你肯定可以看到东西

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-31
        • 2017-03-27
        • 1970-01-01
        • 2011-03-13
        • 2023-03-19
        • 1970-01-01
        相关资源
        最近更新 更多