2013-04-03 23 views
3

我希望我的代碼段中的字符串與中心對齊。對齊谷歌地圖標記片段中的文字

此外,代碼段中的斷路器(\ n)被轉換爲空格,是否有插入斷路器的方法?

我的相關代碼:

GoogleMap map = ... 
map.addMarker(new MarkerOptions().position(pos) 
     .title("title") 
     .snippet("body \n and mind"); 

感謝

+0

您可以定義自己的[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

回答

12

使用一些例子,以及從答案: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; 
     } 
    });