2010-08-23 69 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