自定义信息窗口 -- 图像显示,文本不显示



我想使用InfoWindow的默认设计制作一个显示图像,标题和副标题的信息窗口,从而在我的代码中实现InfoWindowAdapter接口。因为我想保留窗口的默认设计,所以我使用 getInfoContents 方法并在那里设置我的值。但是,当我单击标记时,唯一显示的是图像,而不是文本。我尝试注释掉图像,看看文本是否只是被图像掩盖了,但没有显示任何内容。我的标题和副标题字段未显示在标注中。

但是,当我将相同的代码放入 getInfoWindow 方法中时,所有三个字段都按预期显示。

我宁愿不使用getInfoWindow方法,因为默认标注样式对我来说就足够了。

这是我调用自定义信息适配器的地方:

 private class CustomWindowAdapter implements GoogleMap.InfoWindowAdapter {
    private View infoView;
    CustomWindowAdapter() {
        infoView = getLayoutInflater().inflate(R.layout.custom_map_infoview, null);
    }
    @Override
    public View getInfoContents(Marker marker)
    {
        TextView title = (TextView)infoView.findViewById(R.id.popup_title);
       TextView subtitle = (TextView)infoView.findViewById(R.id.popup_subtitle);
        ImageView image = (ImageView)infoView.findViewById(R.id.popup_image);
        title.setText(marker.getTitle());
        subtitle.setText(marker.getSnippet());
        image.setImageResource(R.drawable.image);
        return infoView;
    }
    @Override
    public View getInfoWindow(Marker marker)
    {
       return null;
    }
}

这是我的布局.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
    android:id="@+id/popup_image"
    android:layout_height="match_parent"
    android:layout_width="match_parent" />
<TextView
    android:id="@+id/popup_title"
    android:layout_height="match_parent"
    android:layout_width="match_parent" />
<TextView
    android:id="@+id/popup_subtitle"
    android:layout_height="match_parent"
    android:layout_width="match_parent" />
</LinearLayout>

关于为什么它可能不会出现的任何想法?

信息窗口的问题之一是,由于View是由另一个进程呈现的,因此您无法轻松使用层次结构视图等工具来查看布局是否按预期工作。使用 IDE 的图形布局编辑器有助于提供可用于诊断问题的预览。

无论如何,在这种情况下,您要求垂直LinearLayout的所有三个子级的高度为 match_parent ,并且没有重量。结果,"第一个获胜",两个TextView小部件的高度将为 0。

最新更新