【问题标题】:Using Raw MapView to Float Buttons使用原始 MapView 浮动按钮
【发布时间】:2012-12-20 17:33:29
【问题描述】:

在新的谷歌地图 api 出现之前,我能够实现一个谷歌地图片段,我会通过将按钮添加到 xml 文件中来添加按钮以在地图上浮动。我可以在原始地图视图演示活动的 xml 文件中使用相同的技术。演示活动是一个片段活动,但如果感觉它无法实际使用片段。它只是用 xml 文件中的地图填充其视图。

是否可以强制地图片段加载特定布局,而无需创建扩展地图片段的片段并且必须自己处理地图视图生命周期。我知道通常不建议这样做。

用于按钮的旧 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" >

来自 rawMapViewDemo 的 XML

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

【问题讨论】:

    标签: android android-button google-maps-android-api-2


    【解决方案1】:

    我试图以编程方式构建我的地图片段,这不允许我做我想做的事。将片段添加到布局文件确实允许我正确添加按钮。现在只是显示和隐藏地图片段以及视图未更新的问题。 下面的 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>
    

    【讨论】:

      猜你喜欢
      • 2021-10-20
      • 1970-01-01
      • 2013-02-17
      • 2016-11-26
      • 2019-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多