2013-05-15 57 views
0

我正在应用程序中显示ContextMenu。现在,当按下此ContextMenu中的某个菜单项时,我想显示一个PopupWindow。我发现了一些如何在互联网上使用PopupWindow的例子,但我仍然有问题。我有以下代码:PopupView.showAtLocation是未定义的类型查看

LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE); 
View popupView = layoutInflater.inflate(R.layout.popup, null); 
final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
popupView.showAtLocation(this.findViewById(R.id.main_layout), Gravity.CENTER, 0, 0); 

以及活动的XML文件中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/main_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="#232323" > 

.... 

</RelativeLayout> 

我想这应该工作,但我得到了以下错误消息:

“的方法showAtLocation (View,int,int,int)未定义为类型View“

这是什么意思?我给showAtLocation一个视图和三个整数,对吧?所以有什么问题?你们有什么想法吗?

回答

1

你用popView调用它。你应该用popupWindow调用它。

这种方法对于popupwindow不是视图

+0

哇感谢您的快速响应!愚蠢的是,我没有看到我自己:)。 – Zero

0

请检查此

popupview.showAtLocation(视图,Gravity.CENTER,0,0);

第一个参数的观点是在当前页面视图,基于该视图位置这个弹出的看法是这条线出现

2
popupView.showAtLocation(this.findViewById(R.id.main_layout), Gravity.CENTER, 0, 0); 

变化

popupWindaow.showAtLocation(popupView,Gravity.CENTER, 0, 0); 

这样。

请注意。您已将showAtLocation设置为Windaow,而不是View

我希望这会对您有所帮助。