使用一些例子,以及從答案:custom info window adapter with custom data in map v2,我遇到了以下解決方案:
marker.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" />
</RelativeLayout>
Java:
map.setInfoWindowAdapter(new InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker arg0) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
View v = getLayoutInflater().inflate(R.layout.marker, null);
TextView info= (TextView) v.findViewById(R.id.info);
info.setText(marker.getPosition().toString());
return v;
}
});
您可以定義自己的[InfoWindowAdapter](https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap.InfoWindowAdapter)。查看[Google Maps Demo](https://developers.google.com/maps/documentation/android/intro#sample_code),您可以在其中找到一個MarkerDemoActivity.java,在這裏他們創建一個CustomInfoWindowAdapter並將一個自定義的.xml文件分配給它。 – lambda