【发布时间】:2010-12-30 09:15:09
【问题描述】:
我在网上查找并找不到 PopupWindow 类的工作示例。我在网上找到的代码示例要么编译但不起作用,要么正在使用已被删除的方法(例如Activity.getViewInflate())。
是否有一个显示 PopupWindow 的简单工作示例?
【问题讨论】:
标签: android android-popupwindow
我在网上查找并找不到 PopupWindow 类的工作示例。我在网上找到的代码示例要么编译但不起作用,要么正在使用已被删除的方法(例如Activity.getViewInflate())。
是否有一个显示 PopupWindow 的简单工作示例?
【问题讨论】:
标签: android android-popupwindow
我基于this Google Groups post.创建了一个工作示例
要创建一个简单的工作 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);
【讨论】:
The popup does not provide any background. This should be handled by the content view.。因此,您的弹出视图布局根应指定一个 android:background="" 属性。
root container 这个词是什么意思?
AFAIK 只有 AbsoluteLayout 有效(请确认),如 http://sree.cc/google/android/android-popup-window 所示。我已经正确显示了弹出窗口,但 LinearLayout 没有显示所有元素。但 AbsoluteLayout 已被弃用!
FrameLayout 也可以使用,但组织视图是一场噩梦,因为官方文档说它只对保持一个视图有用。
另外,为了能够接收触摸事件,您需要这样做:setBackgroundDrawable(new BitmapDrawable());
【讨论】:
你得到了隐形,因为你没有设置你被充气的布局的背景颜色。将它设置为 android:background="#778899",你肯定可以看到东西
【讨论】: