【问题标题】:Android - map with 3 buttonsAndroid - 带有 3 个按钮的地图
【发布时间】:2010-08-23 05:07:11
【问题描述】:

我第一次尝试在 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.地图应该是全屏的,按钮在下方。

我不知道我是怎么做到的?当我们想使用 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>

【问题讨论】:

  • RelativeLayout 是您所需要的。虽然...首先让地图单独出现。看来你还没有完成那部分。一旦你的地图工作了,做按钮的东西就很容易了。
  • @Cristian checkout 我已经给出了我用来显示地图的布局编码..现在我必须做什么?感谢您的帮助

标签: android android-layout android-mapview android-button


【解决方案1】:

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>

【讨论】:

  • 通过这样做,地图底部的 Google 徽标可能不会显示不符合 Google 条款和条件的内容。请记住这一点。
【解决方案2】:

您需要的代码布局如下。

<?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>

【讨论】:

  • 布局代码错误 -> Button01 和 Button03 具有相同的“id”
  • @Paresh Mayani:很抱歉。但布局设计达到你想要的。和克里斯蒂安写了一个最简化的代码。它应该有效。 ;)
  • @Paresh 那是 gr8 和thanx 的支持...我不能多次投票,否则肯定会这样做
猜你喜欢
  • 2014-09-20
  • 2015-01-08
  • 1970-01-01
  • 2023-03-25
  • 2011-12-25
  • 1970-01-01
  • 2016-07-03
  • 1970-01-01
  • 2011-01-04
相关资源
最近更新 更多