Android 如何在MapLongClickListener上弹出窗口?
我想在长时间单击地图时弹出一个窗口,下面是我的代码和popup.xml: 但是弹出窗口没有出现,Android 如何在MapLongClickListener上弹出窗口?,android,google-maps,popupwindow,Android,Google Maps,Popupwindow,我想在长时间单击地图时弹出一个窗口,下面是我的代码和popup.xml: 但是弹出窗口没有出现,日志显示窗口。此外,有许多版本的构造函数和showAtLocation函数,我不太清楚它们之间的区别。有人能找出我代码中的错误吗?多谢各位 private void init() { mGMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)) .getMap(); if( mG
日志
显示窗口。此外,有许多版本的构造函数
和showAtLocation
函数,我不太清楚它们之间的区别。有人能找出我代码中的错误吗?多谢各位
private void init()
{
mGMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
if( mGMap != null )
{
mGMap.setMyLocationEnabled(true);
mGMap.getUiSettings().setMyLocationButtonEnabled(true);
mGMap.getUiSettings().setRotateGesturesEnabled(true);
mGMap.getUiSettings().setCompassEnabled(true);
mGMap. setOnMapLongClickListener (new GoogleMap.OnMapLongClickListener(){
@Override
public void onMapLongClick(LatLng point) {
// TODO Auto-generated method stub
View popup_view = getLayoutInflater().inflate(R.layout.popup, null);
PopupWindow popupWindow = new PopupWindow(popup_view);
View parent = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getView();
WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
@SuppressWarnings("deprecation")
int xoff = windowManager.getDefaultDisplay().getWidth() / 2 - popupWindow.getWidth() / 2;
// 使其聚集
popupWindow.setFocusable(true);
// 设置允许在外点击消失
popupWindow.setOutsideTouchable(false);
popupWindow.showAtLocation(parent, Gravity.CENTER, xoff, 0);
if(popupWindow.isShowing()){
Log.i(mainActivity.toString(), "pop up is showing");
}
popupWindow.update();
}
});
}
下面是我的popup.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/location_time_tag"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="@string/loc_date" />
<ListView
android:id="@+id/listview_diary"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentLeft="true"
android:smoothScrollbar="true"
android:layout_below="@id/location_time_tag">
</ListView>
<EditText
android:id="@+id/add_note"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/listview_diary"
android:layout_alignParentLeft="true"
/>
<Button
android:id="@+id/add_text_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/add_note"
android:layout_alignParentLeft="true"
android:text="写日记"/>
<Button
android:id="@+id/del_text_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/add_text_btn"
android:layout_alignParentRight="true"
android:text="删日记"/>
</RelativeLayout>
由于日志显示弹出窗口已打开,这意味着您为窗口添加了错误的上下文。这意味着窗口已打开,但在其他活动或其他类中。您应该测试是否将正确的上下文应用于窗口。我已尝试了main活动
,但弹出窗口仍然没有显示。可能这不是问题…好吧,然后在添加窗口后在代码中尝试mapview.invalidate(),因为在添加任何查看对象后必须重新绘制地图。我将上下文更改为MapFragment.getActivity()
;最重要的是,我忘记了setHeight()
和setWidth()
,问题解决了。谢谢你的帮助。