2014-02-14 63 views
0

我想使用InfoWindow的默認設計製作一個infowindow,顯示圖像,標題和副標題,從而在我的代碼中實現InfoWindowAdapter接口。因爲我想保留窗口的默認設計,所以我使用getInfoContents方法並在那裏設置我的值。但是,當我點擊標記時唯一顯示的是圖像,而不是文本。我嘗試對圖片進行評論,看看文字是否被圖片埋沒,但沒有任何東西出現。我的標題和副標題字段未顯示在標註中。自定義InfoWindow - 圖像顯示,文本不顯示

但是,當我將相同的代碼放入getInfoWindow方法中時,所有三個字段都按預期顯示。

我寧願不使用getInfoWindow方法,因爲默認的標註樣式對我來說足夠了。

這裏就是我所說的自定義信息適配器:

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

您的佈局有點奇怪。那些孩子的寬度和高度不應該有'wrap_content',而不是'match_parent'? – CommonsWare

+0

@CommonsWare:而且解決了它!噢親愛的。我對Android非常陌生,並沒有考慮到這個原因。把這作爲答案,我會標記它。 – Skitterm

回答

0

信息窗口的一個問題是,由於View正在由另一個進程渲染,因此不能輕易使用像Hierarchy View這樣的工具來查看佈局是否按預期工作。使用IDE的圖形佈局編輯器可以幫助提供可能用於診斷問題的預覽。

無論如何,在這種情況下,您要求垂直LinearLayout的所有三個孩子的身高都是match_parent,且沒有體重。因此,「第一個獲勝」和兩個TextView小部件將以0的高度結束。