我确信我错过了这里的重点,所以我希望有人能解释一下。
我想在用户触摸时创建一个弹出窗口ImageView
。我看了一下AlertDialog
文档说...
如果您想显示更复杂的视图,请查找名为“custom”的 FrameLayout 并将您的视图添加到其中:
...使用以下代码...
FrameLayout fl = (FrameLayout) findViewById(android.R.id.custom);
fl.addView(myView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
因此,作为测试,我在 onCLick() 方法中尝试了以下操作......
TextView tv = new TextView(this);
tv.setText("Hello World");
FrameLayout customFrameLayout = (FrameLayout) findViewById(android.R.id.custom);
customFrameLayout.addView(tv, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
上面我打电话的最后一行addView
抛出一个NullPointerException
这让我觉得有问题android.R.id.custom
.
所以问题是,上面有什么问题,还有没有更好的方法来创建自定义弹出窗口(也许通过使用Dialog
类或扩展它)?
注意:我只使用TextView
在此示例中作为测试,我想为我的实际弹出窗口添加更复杂的内容。