自定义InfoWindow - 图像显示,文本不显示

时间:2014-02-14 17:34:07

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

我想使用InfoWindow的默认设计创建一个显示图像,标题和副标题的信息窗口,从而在我的代码中实现InfoWindowAdapter接口。因为我想保留窗口的默认设计,所以我使用getInfoContents方法并在那里设置我的值。但是,单击标记时唯一显示的是图像,而不是文本。我试着评论出图像,看看文字是否刚被图像掩埋,但没有出现。我的标题和副标题字段未显示在标注中。

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

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

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

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

这是我的layout.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>

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

1 个答案:

答案 0 :(得分:0)

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

无论如何,在这种情况下,您要求垂直LinearLayout的所有三个孩子的身高都为match_parent,并且没有重量。结果,“胜利中的第一个”和两个TextView小部件最终会以0为高。

相关问题