2013-05-13 24 views
18

Đây là cách bố trí tùy chỉnh của tôi cho cửa sổ thông tin của tôi:Làm thế nào để đặt drawable làm nền trên InfoWindow (API Google Maps v2 dành cho Android)?

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

Và đây là bộ chuyển đổi tùy chỉnh của tôi:

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

} 

Và đây là kết quả:

enter image description here

Tuy nhiên Tôi muốn tùy chỉnh drawable làm nền duy nhất cho cửa sổ thông tin của tôi, Làm thế nào để đạt được điều này?

Trả lời

49

Thay thế mã trong getInfoContents với getInfoWindow. Sự khác biệt giữa chúng là getInfoContents kết thúc tốt đẹp View của bạn ở ViewGroup với nền mặc định.

@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

Tôi thực sự đánh giá cao câu trả lời rõ ràng của bạn, tuy nhiên tôi đã nhận được NullPointerException sử dụng RelativeLayout làm gốc trong bố cục tùy chỉnh của tôi để tôi sử dụng LinearLayout thay thế. – IsaacCisneros

+1

@IsaacCisneros Điều đó có lẽ sẽ là câu hỏi ban đầu của bạn sau đó. Đây là một vấn đề được biết đến. Xem phần này: [gmaps-api-issues] (https://code.google.com/p/gmaps-api-issues/issues/detail?id=4748). –

+1

@ MaciejGórski mã thực sự tốt đẹp .... –

0

thử này ..

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; 
     } 
    }); 
Các vấn đề liên quan