Android 单击标记->;getInfoContents不显示我的自定义小部件

Android 单击标记->;getInfoContents不显示我的自定义小部件,android,android-maps-v2,Android,Android Maps V2,安卓工作室: 实现'com.google.android.gms:play services地图:17.0.0' 在我的活动中: import com.google.android.gms.maps.GoogleMap import com.google.android.gms.maps.OnMapReadyCallback private lateinit var mMap: GoogleMap mMap.setInfoWindowAdapter(object : GoogleMap.I

安卓工作室:

实现'com.google.android.gms:play services地图:17.0.0'

在我的活动中:

import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback

private lateinit var mMap: GoogleMap

 mMap.setInfoWindowAdapter(object : GoogleMap.InfoWindowAdapter {
            override fun getInfoWindow(marker: Marker): View? {
                return null
            }

            // Important! Not work with "androidx.constraintlayout.widget.ConstraintLayout"
            override fun getInfoContents(marker: Marker): View? {
                val markerLatLng = marker.position
                val selectGazStation =
                    gazStationsList.first { it.latitude == markerLatLng.latitude && it.longitude == markerLatLng.longitude }
                Debug.d(TAG, "getInfoContents: selectGazStation = $selectGazStation")
                val agentSummaryInfoWidget = AgentSummaryInfoWidget(application, selectGazStation)
                return agentSummaryInfoWidget
            }
        })
    }
这里是我的自定义小部件
AgentSummaryInfoWidget

class AgentSummaryInfoWidget(context: Context, selectGazStation: GazStation) :
    LinearLayout(context) {

    init {
        init(context, selectGazStation)
    }

    companion object {

        private val TAG = AgentSummaryInfoWidget::class.java.name
    }

    private fun init(context: Context, selectGazStation: GazStation) {
        val mapMarkerInfoContentLayoutBinding =
            MapMarkerInfoContentLayoutBinding.inflate(LayoutInflater.from(context));
        mapMarkerInfoContentLayoutBinding.addressValueTextView.setText(selectGazStation.address)
        var workingHours = "24/24"
        if (!selectGazStation.workingHours?.contains("NON-STOP", true)!!) {
            workingHours = selectGazStation.workingHours!!
        }
        mapMarkerInfoContentLayoutBinding.workingHoursValueTextView.setText(workingHours)
        mapMarkerInfoContentLayoutBinding.phoneValueTextView.setText(selectGazStation.phone)
    }
}
这里是xml
map\u marker\u info\u content\u layout

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/titleContainerLayout"
            android:layout_width="match_parent"
            android:layout_height="@dimen/min_height"
            android:layout_marginStart="@dimen/half_default_margin"
            android:layout_marginTop="@dimen/default_margin"
            android:layout_marginEnd="@dimen/half_default_margin"
            android:background="@drawable/bottom_border_bg"
            android:orientation="vertical">

            <TextView
                android:id="@+id/titleTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/half_default_margin"
                android:text="Rompetrol"
                android:textColor="@android:color/black"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/stateTtextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/half_default_margin"
                android:text="@string/open"
                android:textColor="@android:color/holo_green_light"
                android:textSize="14sp"
                app:layout_constraintBottom_toBottomOf="parent" />
        </LinearLayout>


        <LinearLayout
            android:id="@+id/agentInfoContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bottom_border_bg"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/containerAddress"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/half_default_margin"
                android:layout_marginTop="@dimen/half_default_margin"
                android:layout_marginEnd="@dimen/half_default_margin">

                <TextView
                    android:id="@+id/addressLabelTextView"
                    style="@style/mapMarkerInfoItemLabelTextStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="@dimen/half_default_margin"
                    android:layout_marginEnd="@dimen/half_default_margin"
                    android:text="@string/address_colon" />

                <TextView
                    android:id="@+id/addressValueTextView"
                    style="@style/mapMarkerInfoItemLabelTextStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="@dimen/half_default_margin"
                    android:gravity="end" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/containerWorkingHours"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/half_default_margin"
                android:layout_marginTop="@dimen/half_default_margin"
                android:layout_marginEnd="@dimen/half_default_margin">

                <TextView
                    android:id="@+id/workingHoursLabelTextView"
                    style="@style/mapMarkerInfoItemLabelTextStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="@dimen/half_default_margin"
                    android:layout_marginEnd="@dimen/half_default_margin"
                    android:text="@string/working_hours_colon" />

                <TextView
                    android:id="@+id/workingHoursValueTextView"
                    style="@style/mapMarkerInfoItemLabelTextStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="@dimen/half_default_margin"
                    android:gravity="end"
                    app:layout_constraintEnd_toEndOf="parent" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/containerPhon"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/half_default_margin"
                android:layout_marginTop="@dimen/half_default_margin"
                android:layout_marginEnd="@dimen/half_default_margin"
                android:layout_marginBottom="@dimen/half_default_margin">

                <TextView
                    android:id="@+id/phoneLabelTextView"
                    style="@style/mapMarkerInfoItemLabelTextStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="@dimen/half_default_margin"
                    android:layout_marginEnd="@dimen/half_default_margin"
                    android:text="@string/phone_colon" />

                <TextView
                    android:id="@+id/phoneValueTextView"
                    style="@style/mapMarkerInfoItemLabelTextStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="@dimen/half_default_margin"
                    android:gravity="end" />

            </LinearLayout>

        </LinearLayout>

        <LinearLayout
            android:id="@+id/containerServices"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/half_default_margin"
            android:layout_marginTop="@dimen/half_default_margin"
            android:layout_marginEnd="@dimen/half_default_margin"
            android:layout_marginBottom="@dimen/half_default_margin"
            android:orientation="vertical">

            <TextView
                android:id="@+id/servicesLabelTextView"
                style="@style/mapMarkerInfoItemLabelTextStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/half_default_margin"
                android:layout_marginEnd="@dimen/half_default_margin"
                android:text="@string/services_colon" />

            <LinearLayout
                android:id="@+id/gazStationsServicesContainer"
                android:layout_width="match_parent"
                android:layout_height="64dp"
                android:gravity="start|center"
                android:orientation="horizontal">

                <com.yarolegovich.discretescrollview.DiscreteScrollView
                    android:id="@+id/gazStationServiceDiscreteScrollView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:dsv_orientation="horizontal" />

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</layout>

当我点击market时,我的自定义小部件不会显示。结果如下:


您不能直接在标记上浮动自定义视图,您可以通过使用自定义标记(包括自定义布局上方的位置图标)并在地图上使用此布局和写入onclick侦听器来实现显示和隐藏