2013-05-13 83 views
18

這是我爲我的信息窗口自定義佈局:如何在InfoWindow(適用於Android的Google Maps API v2)上將drawable作爲背景?

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

} 

這是結果:

enter image description here

然而我希望自定義可繪製爲我的信息窗口的唯一背景,如何實現這一目標?

回答

49

getInfoContentsgetInfoWindow替換代碼。它們之間的差異是getInfoContents用包含您的View默認背景。

@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 2013-05-13 10:43:15

+1

@IsaacCisneros那應該可能是你原來的問題了。這是一個已知的問題。看到這個:[gmaps-api-issues](https://code.google.com/p/gmaps-api-issues/issues/detail?id=4748)。 – 2013-05-13 10:46:30

+1

@MaciejGórski真的不錯的代碼.... – 2013-07-11 12:26:31

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; 
     } 
    }); 
相關問題