You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

安卓谷歌地图自定义Infowindow中CardView圆角不生效如何解决

问题根因
  • 谷歌地图InfoWindow存在特殊渲染机制:它不会直接渲染你传入的真实View实例,而是将View预先绘制为静态Bitmap后再贴到地图图层上,因此部分View的实时绘制特性(比如CardView的圆角裁剪)不会被默认保留。而Android Studio预览是直接渲染真实View层级,所以会出现预览正常、实际运行无效的差异。
  • 布局结构存在冲突:你当前把CardView背景设为透明,内层LinearLayout却用了实色背景,LinearLayout没有对应圆角设置,实色的直角区域会直接覆盖CardView的圆角,视觉上看不到圆角效果。
  • 没有关闭InfoWindow默认背景:谷歌地图InfoWindow默认自带白色直角背景+底部尖角,默认背景会直接覆盖你自定义布局的圆角区域。
修复步骤

1. 调整自定义布局

将背景色移到CardView上,同时增加CardView圆角兼容配置,避免内层布局盖掉圆角,修改后布局参考:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    card_view:cardBackgroundColor="?attr/fullscreenBackgroundColor"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="24dp"
    card_view:cardElevation="0dp"
    card_view:contentPadding="0dp"
    card_view:cardUseCompatPadding="true"
    card_view:preventCornerOverlap="false">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:theme="@style/ThemeOverlay.FullscreenContainer">

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dp"
            android:gravity="center"
            android:layout_gravity="bottom|center_horizontal"
            android:textColor="@color/white"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:gravity="center"
            android:layout_gravity="bottom|center_horizontal"
            android:textColor="@color/black"
            android:textSize="16sp" />

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/image"
            android:layout_width="300dp"
            android:layout_height="200dp"
            android:layout_margin="4dp"
            android:scaleType="fitXY" />
    </LinearLayout>
</androidx.cardview.widget.CardView>

2. 关闭InfoWindow默认背景

在你的InfoWindowAdapter实现中,优先重写getInfoWindow方法(而非getInfoContents),给返回的View设置透明背景,示例代码如下(Kotlin写法,Java逻辑一致):

map.setInfoWindowAdapter(object : GoogleMap.InfoWindowAdapter {
    override fun getInfoWindow(marker: Marker): View? {
        val infoWindowView = layoutInflater.inflate(R.layout.你的布局文件名, null)
        // 关键:清除默认背景
        infoWindowView.setBackgroundColor(Color.TRANSPARENT)
        // 最低支持版本低于Android 12时可加这行,保证圆角裁剪生效
        infoWindowView.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
        
        // 这里自行绑定标题、描述、图片等数据
        return infoWindowView
    }

    override fun getInfoContents(marker: Marker): View? {
        // 返回null表示完全使用自定义的getInfoWindow布局
        return null
    }
})

3. 可选优化

如果不需要InfoWindow自带的底部尖角,你可以通过自定义谷歌地图样式的方式隐藏尖角,进一步保证圆角效果完整。不存在InfoWindow专属的圆角配置项,所有圆角效果都需要通过自定义布局+适配渲染逻辑实现。

内容的提问来源于stack exchange,提问作者Bungles

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 11:36:04