2010-08-23 20 views
2

我試圖首次在Android中顯示地圖。 現在I want to display 3 buttons on the map, and when I clicked on a particular button, that button's click event should be raised.地圖應該全屏顯示,按鈕位於下方。Android - 帶3個按鈕的地圖

我不知道我是怎麼做到的?當我們想要使用MapView顯示地圖時,我們必須擴展MapActivity類。所以請建議一些想法,例子或參考網站。

編輯:

我曾嘗試使用下面的佈局顯示的地圖:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 

    <com.google.android.maps.MapView 
     android:id="@+id/mapView01" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:enabled="true" 
     android:clickable="true" 
     android:apiKey="my generated api key" 
     /> 

    <Button 
     android:text="Button" 
     android:id="@+id/Button01" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
    </Button> 
</LinearLayout> 
+1

'RelativeLayout'就是你需要的。雖然......首先讓地圖單獨出現。看來你還沒有完成那部分。一旦你有地圖工作,做按鈕的東西很容易。 – Cristian 2010-08-23 05:10:53

+0

@Cristian結帳我給了我用來顯示地圖的佈局編碼..現在我該怎麼辦? thanx的幫助 – 2010-08-23 05:22:05

回答

5

Praveen的回答很好。請記住RelativeLayout的最大優點之一就是可以避免不必要的嵌套。佈局越簡單,維護就越容易。這相當於Praveen的答案:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 
    <com.google.android.maps.MapView 
     xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mapview" 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:clickable="true" android:apiKey="your_id" /> 
    <Button android:layout_below="@+id/mapview" 
     android:text="@+id/Button03" 
     android:id="@+id/Button01" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentBottom="true"/> 
    <Button android:layout_below="@+id/mapview" 
     android:text="@+id/Button02" 
     android:id="@+id/Button02" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_alignParentBottom="true"/> 
    <Button android:text="@+id/Button03" 
     android:id="@+id/Button01" 
     android:layout_width="wrap_content" 
     android:layout_toLeftOf="@+id/mapview" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentBottom="true"/> 
</RelativeLayout> 
+0

你是正確的,併爲答案thanx ...讓我試試吧 – 2010-08-23 17:39:02

+0

通過這樣做谷歌標誌在地圖底部可能沒有顯示哪些不符合Google的條款和條件。記住這一點。 – Klaasvaak 2013-01-28 14:16:02

3

你需要的是下面的代碼佈局。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 
    <com.google.android.maps.MapView 
     xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mapview" 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:clickable="true" android:apiKey="your_id" /> 


    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true"> 
     <Button android:layout_below="@+id/mapview" android:text="@+id/Button03" 
      android:id="@+id/Button01" android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:layout_alignParentLeft="true"></Button> 
     <Button android:layout_below="@+id/mapview" android:text="@+id/Button02" 
      android:id="@+id/Button02" android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:layout_centerInParent="true"></Button> 
     <Button android:text="@+id/Button03" android:id="@+id/Button01" 
      android:layout_width="wrap_content" android:layout_toLeftOf="@+id/mapview" 
      android:layout_height="wrap_content" android:layout_alignParentRight="true"></Button> 
    </RelativeLayout> 





</RelativeLayout> 
+0

thanx的答案 – 2010-08-23 05:58:51

+0

錯誤的佈局代碼 - > Button01和Button03具有相同的「ID」 – 2010-08-25 06:22:55

+0

@Paresh Mayani:對不起。但佈局設計達到你想要的。和Cristion寫一個最簡化的代碼。它應該工作。 ;) – Praveen 2010-08-25 06:37:37