2013-01-07 27 views
1

在新的谷歌地圖API出來之前,我能夠實現一個谷歌地圖片段,我會添加按鈕來浮動在地圖上,將它們添加到XML文件。我可以在Raw map視圖演示活動的xml文件中使用相同的技術。演示活動是一個片段活動,但如果覺得它無法真正使用片段。它只是用來自xml文件的地圖填充其視圖。使用原始MapView浮動按鈕

是否有可能強制地圖片段加載特定的佈局,而無需創建擴展mapfragment的片段並且必須親自處理mapview生命週期。我知道這樣做通常不被推薦。

舊XML的按鈕

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/frame" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<com.google.android.maps.MapView 
    android:id="@+id/mapview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:apiKey="0xR1g8qRRLoUp8-8gd7050zKWtMQCMDDHhizisw" 
    android:clickable="true" > 

    android:apiKey = "APIKEY" > 
</com.google.android.maps.MapView> 

<ZoomControls 
    android:id="@+id/zoom" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:padding="10dp" /> 

<ImageButton 
    android:id="@+id/button" 
    android:layout_width="80dp" 
    android:layout_height="80dp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:background="@null" 
    android:contentDescription="@string/description" 
    android:padding="20dp" 
    android:src="@drawable/button" > 

XML從rawMapViewDemo

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/frame" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<com.google.android.gms.maps.MapView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<ImageButton 
    android:id="@+id/cacbutton" 
    android:layout_width="80dp" 
    android:layout_height="80dp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:background="@null" 
    android:contentDescription="@string/description" 
    android:padding="20dp" 
    android:src="@drawable/taxi" > 
</ImageButton> 

回答

0

我試圖建立我的地圖片段編程不允許我做什麼我想要做。將碎片添加到佈局文件允許我正確添加按鈕。現在只是顯示和隱藏mapfragment並且視圖沒有被更新的問題。 下面的XML是爲我工作的。請注意,我實際上使用了2個地圖片段。我還沒有想出如何爲這兩個片段使用一個按鈕。我對想法持開放態度。

<FrameLayout 
    android:id="@+id/fragment_container" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <fragment 
      android:id="@+id/map1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      class="com.google.android.gms.maps.SupportMapFragment" /> 

     <ImageButton 
      android:id="@+id/cacbutton" 
      android:layout_width="80dp" 
      android:layout_height="80dp" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      android:background="@null" 
      android:contentDescription="@string/description" 
      android:padding="20dp" 
      android:src="@drawable/button" > 
     </ImageButton> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <fragment 
      android:id="@+id/map2" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      class="com.google.android.gms.maps.SupportMapFragment" /> 

     <ImageButton 
      android:id="@+id/cacbutton2" 
      android:layout_width="80dp" 
      android:layout_height="80dp" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      android:background="@null" 
      android:contentDescription="@string/description" 
      android:padding="20dp" 
      android:src="@drawable/button" > 
     </ImageButton> 
    </RelativeLayout> 
</FrameLayout>