如何在Map LongClickListener上弹出窗口?

时间:2013-02-07 12:41:57

标签: android google-maps popupwindow

我想在长时间点击地图时创建一个弹出窗口,这是我的代码和popup.xml: 然而弹出窗口没有出现,Log告诉窗口正在显示。此外,ConstructorshowAtLocation函数有很多版本,我不太清楚它们之间的区别。有人可以在我的代码中找到错误吗?非常感谢你!

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> 

1 个答案:

答案 0 :(得分:0)

因为日志说你的弹出窗口是打开的意味着你正在为window添加错误的上下文。这意味着窗口被打开但是在其他一些活动或其他类中。你应该测试应用适当的上下文到窗口。