如何在InfoWindow上使用可绘制对象作为背景(适用于Android的Google Maps API v2)?

21

这是我的信息窗口的自定义布局:

<RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_infowindow" >
    <LinearLayout
            android:id="@+id/text_box"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        <TextView 
            style="@style/TexTitle"
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    <TextView 
            style="@style/TextDistance"
            android:id="@+id/distance"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</RelativeLayout>

这是我的定制适配器:

public class MapInfoWindowAdapter implements InfoWindowAdapter{

    private LayoutInflater inflater;
    private Context context;

    public MapInfoWindowAdapter(Context context){
        inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        this.context = context;
    }

    @Override
    public View getInfoContents(Marker marker) {

        // Getting view from the layout file
        View v = inflater.inflate(R.layout.map_popup, null);

        TextView title = (TextView) v.findViewById(R.id.title);
        title.setText(marker.getTitle());

        TextView address = (TextView) v.findViewById(R.id.distance);
        address.setText(marker.getSnippet());

        return v;
    }

    @Override
    public View getInfoWindow(Marker arg0) {
        // TODO Auto-generated method stub
        return null;
    }

}

这是结果:

在此输入图像描述

但我想让自定义的可绘制作为我的信息窗口唯一的背景,怎么做?

3个回答

59

getInfoContents中的代码替换为getInfoWindow。它们之间的区别在于,getInfoContents会使用默认背景将您的View包装在ViewGroup中。

@Override
public View getInfoWindow(Marker marker) {

    // Getting view from the layout file
    View v = inflater.inflate(R.layout.map_popup, null);

    TextView title = (TextView) v.findViewById(R.id.title);
    title.setText(marker.getTitle());

    TextView address = (TextView) v.findViewById(R.id.distance);
    address.setText(marker.getSnippet());

    return v;
}

@Override
public View getInfoContents(Marker arg0) {
    // TODO Auto-generated method stub
    return null;
}

2
我非常感谢您澄清的答案,但是在我的自定义布局中,使用RelativeLayout作为根时我遇到了NullPointerException问题,因此我改用了LinearLayout。 - IsaacCisneros
1
@IsaacCisneros 那么你最好重新提出你的原始问题。这是一个已知的问题。请参阅此链接:gmaps-api-issues - MaciejGórski
@MaciejGórski 我使用了相同的代码,它可以正常工作。但是当我再次单击同一窗口时,我会遇到问题,我隐藏了窗口。但在后台中显示的默认信息窗口仍然存在。你已经解决这个问题了吗? - Parthi
浪费了3个小时才找到你的答案。谢谢。 - Khaled Alhayek

0

试试这个..

custom_infowindow.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#80000000" 
android:orientation="vertical">

<ImageView
    android:src="@drawable/ic_launcher"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:padding="10dp"/>

</LinearLayout> 

</LinearLayout>


googleMap.setInfoWindowAdapter(new InfoWindowAdapter() 
{

        public View getInfoWindow(Marker arg0)
        {
            View v = getLayoutInflater().inflate(R.layout.custom_infowindow, null);
            return v;
        }

        public View getInfoContents(Marker arg0) 
        {
           return null;
        }
    });

0

除了已接受的答案,我想提到如果你仍然想要信息窗口气泡,你可以使用这个气泡布局库

然后你可以将气泡布局设置为你正在填充的自定义布局中路线视图的背景。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接